Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
How to: Filter Data

How to: Filter Data

This topic shows how to filter query results. The example returns a collection of SalesOrderHeader objects that represents all online orders more than $2,500. The same example is shown using the following Entity Framework query technologies:

The example in this topic is based on the Adventure Works Sales Model. To run the code in this topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).

Example

The following is the LINQ to Entities example.

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);
    }
}

The following is the Entity SQL example.

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);
    }
}

The following is the query builder method example.

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);
    }
}

Community Additions

ADD
Show:
© 2015 Microsoft