如何:筛选数据(实体框架)

本主题介绍如何筛选查询结果。 该示例返回表示大于 2,500 美元的所有联机订单的 SalesOrderHeader 对象的集合。 下面使用以下实体框架 查询技术演示同一示例:

  • LINQ to Entities

  • Entity SQL 以及 ObjectQuery<T>

  • ObjectQuery<T> 的查询生成器方法

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

示例

以下是 LINQ to Entities 示例。

Using context As New AdventureWorksEntities()
    ' Specify the order amount. 
    Dim orderCost As Integer = 2500

    ' Define a LINQ query that returns only online orders 
    ' more than the specified amount. 
    Dim onlineOrders = From order In context.SalesOrderHeaders _
        Where order.OnlineOrderFlag = True AndAlso order.TotalDue > orderCost _
        Select order

    ' Print order information. 
    For Each onlineOrder In onlineOrders
        Console.WriteLine("Order ID: {0} Order date: ", onlineOrder.SalesOrderID)
        Console.WriteLine("Order date: {0:d}", onlineOrder.OrderDate)
        Console.WriteLine("Order number: {0}", onlineOrder.SalesOrderNumber)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    // Specify the order amount.
    int orderCost = 2500;

    // Define a LINQ query that returns only online orders
    // more than the specified amount.
    var onlineOrders =
        from order in context.SalesOrderHeaders
        where order.OnlineOrderFlag == true &&
        order.TotalDue > orderCost
        select order;

    // Print order information.
    foreach (var onlineOrder in onlineOrders)
    {
        Console.WriteLine("Order ID: {0} Order date: "
            + "{1:d} Order number: {2}",
            onlineOrder.SalesOrderID,
            onlineOrder.OrderDate,
            onlineOrder.SalesOrderNumber);
    }
}

以下是 Entity SQL 示例。

Using context As New AdventureWorksEntities()
    ' Specify the order amount. 
    Dim orderCost As Decimal = 2500

    ' Specify the Entity SQL query that returns only online orders 
    ' more than the specified amount. 
    Dim queryString As String = "SELECT VALUE o FROM AdventureWorksEntities.SalesOrderHeaders AS o " & _
                                " WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost"

    ' Define an ObjectQuery and pass the maxOrderCost parameter. 
    Dim onlineOrders As New ObjectQuery(Of SalesOrderHeader)(queryString, context)
    onlineOrders.Parameters.Add(New ObjectParameter("ordercost", orderCost))

    ' Print order information. 
    For Each onlineOrder In onlineOrders
        Console.WriteLine("Order ID: {0} Order date: {1:d} Order number: {2}", _
                          onlineOrder.SalesOrderID, onlineOrder.OrderDate, onlineOrder.SalesOrderNumber)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    // Specify the order amount.
    decimal orderCost = 2500;

    // Specify the Entity SQL query that returns only online orders
    // more than the specified amount.
    string queryString = @"SELECT VALUE o FROM AdventureWorksEntities.SalesOrderHeaders AS o 
        WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost";

    // Define an ObjectQuery and pass the maxOrderCost parameter.
    ObjectQuery<SalesOrderHeader> onlineOrders =
        new ObjectQuery<SalesOrderHeader>(queryString, context);
    onlineOrders.Parameters.Add(
        new ObjectParameter("ordercost", orderCost));

    // Print order information.
    foreach (var onlineOrder in onlineOrders)
    {
        Console.WriteLine("Order ID: {0} Order date: "
            + "{1:d} Order number: {2}",
            onlineOrder.SalesOrderID,
            onlineOrder.OrderDate,
            onlineOrder.SalesOrderNumber);
    }
}

以下是查询生成器方法示例。

Using context As New AdventureWorksEntities()
    ' Specify the order amount. 
    Dim orderCost As Integer = 2500

    ' Define an ObjectQuery that returns only online orders 
    ' more than the specified amount. 
    Dim onlineOrders As ObjectQuery(Of SalesOrderHeader) = _
        context.SalesOrderHeaders.Where("it.OnlineOrderFlag = TRUE AND it.TotalDue > @ordercost", _
                                        New ObjectParameter("ordercost", orderCost))

    ' Print order information. 
    For Each onlineOrder In onlineOrders
        Console.WriteLine("Order ID: {0} Order date: {1:d} Order number: {2}", _
                          onlineOrder.SalesOrderID, onlineOrder.OrderDate, onlineOrder.SalesOrderNumber)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    // Specify the order amount.
    int orderCost = 2500;

    // Define an ObjectQuery that returns only online orders 
    // more than the specified amount.
    ObjectQuery<SalesOrderHeader> onlineOrders =
        context.SalesOrderHeaders
        .Where("it.OnlineOrderFlag = TRUE AND it.TotalDue > @ordercost",
        new ObjectParameter("ordercost", orderCost));

    // Print order information.
    foreach (var onlineOrder in onlineOrders)
    {
        Console.WriteLine("Order ID: {0} Order date: "
           + "{1:d} Order number: {2}",
           onlineOrder.SalesOrderID,
           onlineOrder.OrderDate,
           onlineOrder.SalesOrderNumber);
    }
}