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

[This page is specific to the Entity Framework version 6. The latest version is available as the 'Entity Framework' NuGet package. For more information about Entity Framework, see msdn.com/data/ef.]

Specifies the related objects to include in the query results.

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

Syntax

'Declaration
<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")> _
<SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")> _
<ExtensionAttribute> _
Public Shared Function Include(Of T, TProperty) ( _
    source As IQueryable(Of T), _
    path As Expression(Of Func(Of T, TProperty)) _
) As IQueryable(Of T)
'Usage
Dim source As IQueryable(Of T)
Dim path As Expression(Of Func(Of T, TProperty))
Dim returnValue As IQueryable(Of T)

returnValue = source.Include(path)
[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
)
[SuppressMessageAttribute(L"Microsoft.Design", L"CA1006:DoNotNestGenericTypesInMemberSignatures")]
[SuppressMessageAttribute(L"Microsoft.Design", L"CA1011:ConsiderPassingBaseTypesAsParameters")]
[ExtensionAttribute]
public:
generic<typename T, typename TProperty>
static IQueryable<T>^ Include(
    IQueryable<T>^ source, 
    Expression<Func<T, TProperty>^>^ path
)
[<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")>]
[<SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")>]
static member Include : 
        source:IQueryable<'T> * 
        path:Expression<Func<'T, 'TProperty>> -> IQueryable<'T> 
JScript does not support generic types and methods.

Type Parameters

  • T
    The type of entity being queried.
  • TProperty
    The type of navigation property being included.

Parameters

Return Value

Type: System.Linq.IQueryable<T>
A new IQueryable&lt;T&gt; 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 https://msdn.microsoft.com/en-us/library/bb384936(v=vs.113) or https://msdn.microsoft.com/en-us/library/bb383977(v=vs.113).

Remarks

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.

See Also

Reference

QueryableExtensions Class

Include Overload

System.Data.Entity Namespace