Export (0) Print
Expand All

DbExtensions.Include<T, TProperty> Method (IQueryable<T>, Expression<Func<T, TProperty>>)

Entity Framework 5.0

Specifies the related objects to include in the query results.

Namespace:  System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)

public static IQueryable<T> Include<T, TProperty>(
	this IQueryable<T> source,
	Expression<Func<T, TProperty>> path
)
where T : class

Type Parameters

T

The type of the entity being queried.

TProperty

The type of the navigation property being included.

Parameters

source
Type: System.Linq.IQueryable<T>

The source IQueryable on which to call Include.

path
Type: System.Linq.Expressions.Expression<Func<T, TProperty>>

A lambda expression representing the path to include.

Return Value

Type: System.Linq.IQueryable<T>
A new IQueryable of T with the defined query path.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IQueryable<T>. When you use instance method syntax to call this method, omit the first parameter. For more information, see http://msdn.microsoft.com/en-us/library/bb384936(v=vs.103).aspx or http://msdn.microsoft.com/en-us/library/bb383977(v=vs.103).aspx.

The path expression must be composed of simple property access expressions together with calls to Select in order to compose additional includes after including a collection property. Examples of possible include paths are:

  • To include a single reference: query.Include(e => e.Level1Reference).

  • To include a single collection: query.Include(e => e.Level1Collection).

  • To include a reference and then a reference one level down: query.Include(e => e.Level1Reference.Level2Reference).

  • To include a reference and then a collection one level down: query.Include(e => e.Level1Reference.Level2Collection).

  • To include a collection and then a reference one level down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference)).

  • To include a collection and then a collection one level down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection)).

  • To include a collection and then a reference one level down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference)).

  • To include a collection and then a collection one level down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection)).

  • To include a collection, a reference, and a reference two levels down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference.Level3Reference)).

  • To include a collection, a collection, and a reference two levels down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection.Select(l2 => l2.Level3Reference))).

This extension method calls the Include(String) method of the IQueryable source object, if such a method exists. If the source IQueryable does not have a matching method, then this method does nothing.

When you call the Include method, the query path is only valid on the returned instance of the IQueryable of T. Other instances of IQueryable of T and the context itself are not affected. You can call this method multiple times on an IQueryable of T to specify multiple paths for the query.

Show:
© 2014 Microsoft