Search code examples
c#entity-frameworkstrong-typing

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


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.


Solution

  • 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)).