如何:查询 EntityCollection 中的相关对象(实体框架)

本主题提供的示例演示如何查询由关系导航属性返回的 EntityCollection 中的相关对象。

本主题中的示例基于 Adventure Works 销售模型。若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用 实体框架。为此,请完成如何:手动配置实体框架项目如何:手动定义实体数据模型(实体框架) 中的过程。也可以使用实体数据模型向导定义 AdventureWorks 销售模型。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)

示例

本示例加载与特定联系人相关的 SalesOrderHeader 对象集合,然后使用 LINQ 表达式返回已经发货的在线订单的列表。

' Specify the customer ID.
Dim customerId As Integer = 4332

Using context As AdventureWorksEntities = _
    New AdventureWorksEntities()
    Try
        ' Get a specified customer by contact ID.
        Dim customer = (From customers In context.Contact _
            Where customers.ContactID = customerId _
            Select customers).First()

        ' Load the customer orders if not already loaded.
        If Not customer.SalesOrderHeader.IsLoaded Then
            customer.SalesOrderHeader.Load()
        End If

        ' Write the number of orders for the customer.
        Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
            customer.LastName, customer.SalesOrderHeader.Count)

        ' Get the online orders that have shipped.
        Dim shippedOrders = _
        From order In customer.SalesOrderHeader _
            Where order.OnlineOrderFlag = True _
            And order.Status = 5 _
            Select order

        ' Write the number of orders placed online.
        Console.WriteLine("{0} orders placed online have been shipped.", _
            shippedOrders.Count())
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
// Specify the customer ID.
int customerId = 4332;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Get a specified customer by contact ID.
        var customer = (from customers in context.Contact
            where customers.ContactID == customerId 
            select customers).First();
        
        // Load the customer orders if not already loaded.
        if (!customer.SalesOrderHeader.IsLoaded)
        {
            customer.SalesOrderHeader.Load();
        }

        // Write the number of orders for the customer.
        Console.WriteLine("Customer '{0}' has placed {1} total orders.",
            customer.LastName, customer.SalesOrderHeader.Count);

        // Get the online orders that have shipped.
        var shippedOrders =
            from order in customer.SalesOrderHeader
            where order.OnlineOrderFlag == true
            && order.Status == 5
            select order;

        // Write the number of orders placed online.
        Console.WriteLine("{0} orders placed online have been shipped.",
            shippedOrders.Count());
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

本示例使用与针对 SalesOrderHeader 对象集合的第一个示例相同 LINQ 查询。与最初将所有相关对象加载到集合不同,CreateSourceQuery 方法用于仅加载查询返回的对象。然后在 SalesOrderHeader 关系导航属性返回的 EntityCollection 上调用 Load 方法,用于加载其他相关对象。

' Specify the customer ID.
Dim customerId As Integer = 4332

Using context As AdventureWorksEntities = _
    New AdventureWorksEntities()
    Try
        ' Get a specified customer by contact ID.
        Dim customer = (From customers In context.Contact _
                        Where customers.ContactID = customerId _
                        Select customers).First()

        ' Use CreateSourceQuery to generate a query that returns 
        ' only the online orders that have shipped.
        Dim shippedOrders = _
        From orders In customer.SalesOrderHeader.CreateSourceQuery() _
            Where orders.OnlineOrderFlag = True _
            And orders.Status = 5 _
            Select orders

        ' Write the number of orders placed online.
        Console.WriteLine("{0} orders placed online have been shipped.", _
            shippedOrders.Count())

        ' Load the remaining orders for this customer.
        If Not customer.SalesOrderHeader.IsLoaded Then
            customer.SalesOrderHeader.Load()
        End If

        ' Write the number of total orders for the customer.
        Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
            customer.LastName, customer.SalesOrderHeader.Count)

    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
// Specify the customer ID.
int customerId = 4332;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Get a specified customer by contact ID.
        var customer = (from customers in context.Contact
                        where customers.ContactID == customerId
                        select customers).First();

        // Use CreateSourceQuery to generate a query that returns 
        // only the online orders that have shipped.
        var shippedOrders =
            from orders in customer.SalesOrderHeader.CreateSourceQuery()
            where orders.OnlineOrderFlag == true
            && orders.Status == 5
            select orders;

        // Write the number of orders placed online.
        Console.WriteLine("{0} orders placed online have been shipped.",
            shippedOrders.Count());

        // Load the remaining orders for this customer.
        if (!customer.SalesOrderHeader.IsLoaded)
        {
            customer.SalesOrderHeader.Load();
        }

        // Write the number of total orders for the customer.
        Console.WriteLine("Customer '{0}' has placed {1} total orders.",
            customer.LastName, customer.SalesOrderHeader.Count);
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

另请参见

任务

如何:执行返回实体类型的查询(实体框架)
如何:使用查询路径调整结果(实体框架)
如何:使用导航属性导航关系(实体框架)

概念

形成查询结果(实体框架)