QueryableExtensions.Include Method

 

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

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticInclude(IQueryable, String)

Specifies the related objects to include in the query results.

System_CAPS_pubmethodSystem_CAPS_staticInclude<T>(IQueryable<T>, String)

Specifies the related objects to include in the query results.

System_CAPS_pubmethodSystem_CAPS_staticInclude<T, TProperty>(IQueryable<T>, Expression<Func<T, TProperty>>)

Specifies the related objects to include in the query results.


QueryableExtensions.Include Method (IQueryable, String)

Specifies the related objects to include in the query results.

public static IQueryable Include(
	this IQueryable source,
	string path
)

Parameters

source
Type: System.Linq.IQueryable

The source IQueryable on which to call Include.

path
Type: System.String

The dot-separated list of related objects to return in the query results.

Return Value

Type: System.Linq.IQueryable

A new IQueryable with the defined query path.

This extension method calls the Include(String) method of the source IQueryable object, if such a method exists. If the source IQueryable does not have a matching method, then this method does nothing. The ObjectQuery, ObjectSet<TEntity>, DbQuery and DbSet types all have an appropriate Include method to call. Paths are all-inclusive. For example, if an include call indicates Include("Orders.OrderLines"), not only will OrderLines be included, but also Orders. When you call the Include method, the query path is only valid on the returned instance of the IQueryable. Other instances of IQueryable and the object context itself are not affected. Because the Include method returns the query object, you can call this method multiple times on an IQueryable to specify multiple paths for the query.


QueryableExtensions.Include<T> Method (IQueryable<T>, String)

Specifies the related objects to include in the query results.

public static IQueryable<T> Include<T>(
	this IQueryable<T> source,
	string path
)

Parameters

source
Type: System.Linq.IQueryable<T>

The source IQueryable<T> on which to call Include.

path
Type: System.String

The dot-separated list of related objects to return in the query results.

Return Value

Type: System.Linq.IQueryable<T>

A new IQueryable<T> with the defined query path.

Type Parameters

T

The type of entity being queried.

This extension method calls the Include(String) method of the source IQueryable<T> object, if such a method exists. If the source IQueryable<T> does not have a matching method, then this method does nothing. The ObjectQuery<T>, ObjectSet<TEntity>, DbQuery<TResult> and DbSet<TEntity> types all have an appropriate Include method to call. Paths are all-inclusive. For example, if an include call indicates Include("Orders.OrderLines"), not only will OrderLines be included, but also Orders. When you call the Include method, the query path is only valid on the returned instance of the IQueryable<T>. Other instances of IQueryable<T> and the object context itself are not affected. Because the Include method returns the query object, you can call this method multiple times on an IQueryable<T> to specify multiple paths for the query.


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

Specifies the related objects to include in the query results.

[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
[SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
public static IQueryable<T> Include<T, TProperty>(
	this IQueryable<T> source,
	Expression<Func<T, TProperty>> path
)

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&lt;T&gt; with the defined query path.

Type Parameters

T

The type of entity being queried.

TProperty

The type of navigation property being included.

The path expression must be composed of simple property access expressions together with calls to Select for composing additional includes after including a collection proprty. Examples of possible include paths are: To include a single reference: query.Include(e =&amp;gt; e.Level1Reference) To include a single collection: query.Include(e =&amp;gt; e.Level1Collection) To include a reference and then a reference one level down: query.Include(e =&amp;gt; e.Level1Reference.Level2Reference) To include a reference and then a collection one level down: query.Include(e =&amp;gt; e.Level1Reference.Level2Collection) To include a collection and then a reference one level down: query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Reference)) To include a collection and then a collection one level down: query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Collection)) To include a collection and then a reference one level down: query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Reference)) To include a collection and then a collection one level down: query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Collection)) To include a collection, a reference, and a reference two levels down: query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Reference.Level3Reference)) To include a collection, a collection, and a reference two levels down: query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Collection.Select(l2 =&amp;gt; l2.Level3Reference))) This extension method calls the Include(String) method of the source IQueryable object, if such a method exists. If the source IQueryable does not have a matching method, then this method does nothing. The Entity Framework ObjectQuery, ObjectSet, DbQuery, and DbSet types all have an appropriate Include method to call. When you call the Include method, the query path is only valid on the returned instance of the IQueryable&amp;lt;T&amp;gt;. Other instances of IQueryable&amp;lt;T&amp;gt; and the object context itself are not affected. Because the Include method returns the query object, you can call this method multiple times on an IQueryable&amp;lt;T&amp;gt; to specify multiple paths for the query.