EntityFrameworkQueryableExtensions.Include Method

Definition

Overloads

Include<TEntity,TProperty>(IQueryable<TEntity>, Expression<Func<TEntity,TProperty>>)

Specifies related entities to include in the query results. The navigation property to be included is specified starting with the type of entity being queried (TEntity). If you wish to include additional types based on the navigation properties of the type being included, then chain a call to ThenInclude<TEntity,TPreviousProperty,TProperty>(IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>, Expression<Func<TPreviousProperty, TProperty>>) after this call.

Include<TEntity>(IQueryable<TEntity>, String)

Specifies related entities to include in the query results. The navigation property to be included is specified starting with the type of entity being queried (TEntity). Further navigation properties to be included can be appended, separated by the '.' character.

Include<TEntity,TProperty>(IQueryable<TEntity>, Expression<Func<TEntity,TProperty>>)

Specifies related entities to include in the query results. The navigation property to be included is specified starting with the type of entity being queried (TEntity). If you wish to include additional types based on the navigation properties of the type being included, then chain a call to ThenInclude<TEntity,TPreviousProperty,TProperty>(IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>, Expression<Func<TPreviousProperty, TProperty>>) after this call.

public static Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<TEntity,TProperty> Include<TEntity,TProperty> (this System.Linq.IQueryable<TEntity> source, System.Linq.Expressions.Expression<Func<TEntity,TProperty>> navigationPropertyPath) where TEntity : class;
static member Include : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * System.Linq.Expressions.Expression<Func<'Entity, 'Property>> -> Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<'Entity, 'Property (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function Include(Of TEntity As Class, TProperty As Class) (source As IQueryable(Of TEntity), navigationPropertyPath As Expression(Of Func(Of TEntity, TProperty))) As IIncludableQueryable(Of TEntity, TProperty)

Type Parameters

TEntity

The type of entity being queried.

TProperty

The type of the related entity to be included.

Parameters

source
IQueryable<TEntity>

The source query.

navigationPropertyPath
Expression<Func<TEntity,TProperty>>

A lambda expression representing the navigation property to be included (t => t.Property1).

Returns

A new query with the related data included.

Exceptions

source or navigationPropertyPath is null.

Examples

The following query shows including a single level of related entities:

context.Blogs.Include(blog => blog.Posts)

The following query shows including two levels of entities on the same branch:

context.Blogs
   .Include(blog => blog.Posts).ThenInclude(post => post.Tags)

The following query shows including multiple levels and branches of related data:

context.Blogs
   .Include(blog => blog.Posts).ThenInclude(post => post.Tags).ThenInclude(tag => tag.TagInfo)
   .Include(blog => blog.Contributors)

The following query shows including a single level of related entities on a derived type using casting:

context.Blogs.Include(blog => ((SpecialBlog)blog).SpecialPosts)

The following query shows including a single level of related entities on a derived type using 'as' operator:

context.Blogs.Include(blog => (blog as SpecialBlog).SpecialPosts)

Remarks

See Loading related entities for more information and examples.

Applies to

Include<TEntity>(IQueryable<TEntity>, String)

Specifies related entities to include in the query results. The navigation property to be included is specified starting with the type of entity being queried (TEntity). Further navigation properties to be included can be appended, separated by the '.' character.

public static System.Linq.IQueryable<TEntity> Include<TEntity> (this System.Linq.IQueryable<TEntity> source, string navigationPropertyPath) where TEntity : class;
static member Include : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * string -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function Include(Of TEntity As Class) (source As IQueryable(Of TEntity), navigationPropertyPath As String) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of entity being queried.

Parameters

source
IQueryable<TEntity>

The source query.

navigationPropertyPath
String

A string of '.' separated navigation property names to be included.

Returns

IQueryable<TEntity>

A new query with the related data included.

Exceptions

source or navigationPropertyPath is null.

navigationPropertyPath is empty or whitespace.

Examples

The following query shows including a single level of related entities:

context.Blogs.Include("Posts")

The following query shows including two levels of entities on the same branch:

context.Blogs.Include("Posts.Tags")

The following query shows including multiple levels and branches of related data:

context.Blogs
   .Include("Posts.Tags.TagInfo')
   .Include("Contributors")

Remarks

See Loading related entities for more information and examples.

Applies to