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

[本页针对的是实体框架版本 6。最新版本以“实体框架”NuGet 包的形式提供。有关实体框架的更多信息,请参见 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-cn/library/bb384936(v=vs.113)https://msdn.microsoft.com/zh-cn/library/bb383977(v=vs.113)

备注

路径表达式必须由简单的属性访问表达式与对 Select 的调用组成,以便在包括集合属性后组合其他包含内容。 可能的包含路径示例为:包括单个引用: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 命名空间