EntityFrameworkQueryableExtensions.Include 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Include<TEntity,TProperty>(IQueryable<TEntity>, Expression<Func<TEntity,TProperty>>) |
指定相關實體,包含在查詢結果中。 要包含的導航屬性會從被查詢 |
| Include<TEntity>(IQueryable<TEntity>, String) |
指定相關實體,包含在查詢結果中。 要包含的導航屬性會從被查詢 |
Include<TEntity,TProperty>(IQueryable<TEntity>, Expression<Func<TEntity,TProperty>>)
指定相關實體,包含在查詢結果中。 要包含的導航屬性會從被查詢TEntity的實體類型()開始指定。 如果你想根據所包含型別的導航屬性加入更多類型,請在此呼叫後串接呼叫至 ThenInclude<TEntity,TPreviousProperty,TProperty>(IIncludableQueryable<TEntity,
IEnumerable<TPreviousProperty>>, Expression<Func<TPreviousProperty,
TProperty>>) 。
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)
類型參數
- TEntity
被查詢的實體類型。
- TProperty
所包含的相關實體類型。
參數
- source
- IQueryable<TEntity>
來源查詢。
- navigationPropertyPath
- Expression<Func<TEntity,TProperty>>
一個表示要包含的導航屬性的 lambda 表達式(t => t.Property1)。
傳回
一個包含相關資料的新查詢。
例外狀況
source 或 navigationPropertyPath 為 null。
範例
以下查詢顯示包含單一層級的相關實體:
context.Blogs.Include(blog => blog.Posts)
以下查詢顯示同一分支包含兩個層級的實體:
context.Blogs
.Include(blog => blog.Posts).ThenInclude(post => post.Tags)
以下查詢顯示包含多個層級與分支的相關資料:
context.Blogs
.Include(blog => blog.Posts).ThenInclude(post => post.Tags).ThenInclude(tag => tag.TagInfo)
.Include(blog => blog.Contributors)
以下查詢顯示如何利用鑄造在衍生型態上包含單一層級的相關實體:
context.Blogs.Include(blog => ((SpecialBlog)blog).SpecialPosts)
以下查詢顯示如何利用 'as' 運算子在派生型態上包含單一層級的相關實體:
context.Blogs.Include(blog => (blog as SpecialBlog).SpecialPosts)
備註
更多資訊與範例請參見 載入相關實體 。
適用於
Include<TEntity>(IQueryable<TEntity>, String)
指定相關實體,包含在查詢結果中。 要包含的導航屬性會從被查詢TEntity的實體類型()開始指定。 其他需包含的導航屬性可附加,並以 '.' 字元分隔。
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)
類型參數
- TEntity
被查詢的實體類型。
參數
- source
- IQueryable<TEntity>
來源查詢。
- navigationPropertyPath
- String
一串分隔導航屬性名稱的「.」字串。
傳回
一個包含相關資料的新查詢。
例外狀況
source 或 navigationPropertyPath 為 null。
navigationPropertyPath 為空或留白。
範例
以下查詢顯示包含單一層級的相關實體:
context.Blogs.Include("Posts")
以下查詢顯示同一分支包含兩個層級的實體:
context.Blogs.Include("Posts.Tags")
以下查詢顯示包含多個層級與分支的相關資料:
context.Blogs
.Include("Posts.Tags.TagInfo')
.Include("Contributors")
備註
更多資訊與範例請參見 載入相關實體 。