DbQuery.Include() method: Is there a strong-typed variant?
Asked Answered
E

2

12

This is EF6. I can include .Include() method (no puns intended) in my queries to eager-load information from related tables. However it looks like .Include() method accepts a string parameter only. Is there a way to do it in a strongly-typed way? So for example, instead of writing MyContext.catalog_item.Include("picture"), I could write something like MyDB.catalog_item.Include(i => i.picture) to gain advantages like intellisense and all that.

Erskine answered 17/4, 2014 at 14:43 Comment(0)
G
16

Yep, there is a strongly typed variant in System.Data.Entity

Usage is

.Include(i => i.Property)

The reference page gives examples on how to include collections and properties on collections as well.

Example:

query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference)).
Godship answered 17/4, 2014 at 14:45 Comment(0)
E
0

There's IQueryable extention method added in EF 5:

http://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx

You may want to have a look at how to perform includes of nested navigation properties

Eal answered 17/4, 2014 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.