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

[此頁面專屬於 Entity Framework 第 6 版。最新版本可從 'Entity Framework' NuGet 套件取得。如需 Entity Framework 的詳細資訊,請參閱 msdn.com/data/ef。]

指定要包含在查詢結果中的相關物件。

命名空間:  System.Data.Entity
組件:  EntityFramework (在 EntityFramework.dll 中)

語法

'宣告
<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)
'用途
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.

類型參數

  • T
    正在查詢之實體的類型。
  • TProperty
    所包含之導覽屬性的類型。

參數

傳回值

類型:System.Linq.IQueryable<T>
新的 IQueryable&lt;T&gt;,包含定義的查詢路徑。

使用注意事項

在 Visual Basic 和 C# 中,您可以在任何 IQueryable<T> 類型物件中呼叫這個方法以做為執行個體。使用執行個體方法語法呼叫這個方法時,請省略第一個參數。如需詳細資訊,請參閱 https://msdn.microsoft.com/zh-tw/library/bb384936(v=vs.113)https://msdn.microsoft.com/zh-tw/library/bb383977(v=vs.113)

備註

路徑運算式必須由簡單的屬性存取運算式連同 Select 的呼叫所組成,以便在包含集合屬性之後撰寫其他包含內容。 可能的 Include 路徑範例如下:若要包含單一參考:query.Include(e =&amp;gt; e.Level1Reference) 若要包含單一集合:query.Include(e =&amp;gt; e.Level1Collection) 若要包含參考以及下一層的參考:query.Include(e =&amp;gt; e.Level1Reference.Level2Reference) 若要包含參考以及下一層的集合:query.Include(e =&amp;gt; e.Level1Reference.Level2Collection) 若要包含集合以及下一層的參考:query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Reference)) 若要包含集合以及下一層的集合:query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Collection)) 若要包含集合以及下一層的參考:query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Reference)) 若要包含集合以及下一層的集合:query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Collection)) 若要包含集合、參考以及下兩層的參考:query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Reference.Level3Reference)) 若要包含集合、集合以及下兩層的參考:query.Include(e =&amp;gt; e.Level1Collection.Select(l1 =&amp;gt; l1.Level2Collection.Select(l2 =&amp;gt; l2.Level3Reference))) 此擴充方法會呼叫來源 IQueryable 物件的 Include(String) 方法 (如果有這類方法存在)。 如果來源 IQueryable 沒有符合的方法,此方法不會做任何事。 Entity Framework ObjectQuery、ObjectSet、DbQuery 和 DbSet 類型都有要呼叫的適當 Include 方法。 當您呼叫 Include 方法時,查詢路徑只針對傳回的 IQueryable&amp;lt;T&amp;gt; 執行個體才有效。 其他 IQueryable&amp;lt;T&amp;gt; 執行個體和物件內容本身則不會受到影響。 由於 Include 方法會傳回查詢物件,因此您可以針對 IQueryable&amp;lt;T&amp;gt; 多次呼叫此方法來指定查詢的多個路徑。

請參閱

參考

QueryableExtensions 類別

Include 多載

System.Data.Entity 命名空間