共用方式為


Method-Based Query Syntax Examples: Restriction (LINQ to Entities)

The examples in this topic demonstrate how to use the Where method to query the AdventureWorks Sales Model using method-based query syntax. The AdventureWorks Sales model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

The examples in this topic use the following using/Imports statements:

Option Explicit On
Option Strict On
Imports L2EExamplesVB.AdventureWorksModel
Imports System.Data.Objects
Imports System.Globalization
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using AdventureWorksModel;
using System.Globalization;

For more information, see How to: Create a LINQ to Entities Project in Visual Studio.

Where

Example

The following example returns all online orders.

Using AWEntities As New AdventureWorksEntities
    Dim onlineOrders = AWEntities.SalesOrderHeader _
        .Where(Function(order) order.OnlineOrderFlag = True) _
        .Select(Function(order) New With { _
           .SalesOrderID = order.SalesOrderID, _
           .OrderDate = order.OrderDate, _
           .SalesOrderNumber = order.SalesOrderNumber _
        })

    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 AWEntities = new AdventureWorksEntities())
{
    var onlineOrders = AWEntities.SalesOrderHeader
        .Where(order => order.OnlineOrderFlag == true)
        .Select(s => new { s.SalesOrderID, s.OrderDate, s.SalesOrderNumber });

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

Example

The following example returns the orders where the order quantity is greater than 2 and less than 6.

Using AWEntities As New AdventureWorksEntities
    Dim query = AWEntities.SalesOrderDetail _
        .Where(Function(order) order.OrderQty > 2 And order.OrderQty < 6) _
        .Select(Function(order) New With { _
            .SalesOrderID = order.SalesOrderID, _
            .OrderQty = order.OrderQty _
        })

    For Each order In query
        Console.WriteLine("Order ID: {0} Order quantity: {1}", _
                order.SalesOrderID, order.OrderQty)
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    var query = AWEntities.SalesOrderDetail
        .Where(order => order.OrderQty > 2 && order.OrderQty < 6)
        .Select(s => new { s.SalesOrderID, s.OrderQty });

    foreach (var order in query)
    {
        Console.WriteLine("Order ID: {0} Order quantity: {1}",
            order.SalesOrderID, order.OrderQty);
    }
}

Example

The following example returns all red colored products.

Using AWEntities As New AdventureWorksEntities
    Dim query = AWEntities.Product _
        .Where(Function(product) product.Color = "Red") _
        .Select(Function(product) New With { _
            .Name = product.Name, _
            .ProductNumber = product.ProductNumber, _
            .ListPrice = product.ListPrice _
        })

    For Each product In query
        Console.WriteLine("Name: {0}", product.Name)
        Console.WriteLine("Product number: {0}", product.ProductNumber)
        Console.WriteLine("List price: ${0}", product.ListPrice)
        Console.WriteLine("")
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    var query = AWEntities.Product
        .Where(product => product.Color == "Red")
        .Select(p => new { p.Name, p.ProductNumber, p.ListPrice });

    foreach (var product in query)
    {
        Console.WriteLine("Name: {0}", product.Name);
        Console.WriteLine("Product number: {0}", product.ProductNumber);
        Console.WriteLine("List price: ${0}", product.ListPrice);
        Console.WriteLine("");
    }
}

Example

The following example uses the Where method to find orders that were made after December 1, 2003 and then uses the order.SalesOrderDetail navigation property to get the details for each order.

Using AWEntities As New AdventureWorksEntities
    Dim query = AWEntities.SalesOrderHeader _
        .Where(Function(order) order.OrderDate >= New DateTime(2003, 12, 1)) _
        .Select(Function(order) order)

    Console.WriteLine("Orders that were made after December 1, 2003:")
    For Each order In query
        Console.WriteLine("OrderID {0} Order date: {1:d} ", _
                order.SalesOrderID, order.OrderDate)
        For Each orderDetail In order.SalesOrderDetail
            Console.WriteLine("  Product ID: {0} Unit Price {1}", _
                orderDetail.ProductID, orderDetail.UnitPrice)
        Next
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    IQueryable<SalesOrderHeader> query = AWEntities.SalesOrderHeader
        .Where(order => order.OrderDate >= new DateTime(2003, 12, 1))
        .Select(o => o);

    Console.WriteLine("Orders that were made after December 1, 2003:");
    foreach (SalesOrderHeader order in query)
    {
        Console.WriteLine("OrderID {0} Order date: {1:d} ",
            order.SalesOrderID, order.OrderDate);
        foreach (SalesOrderDetail orderDetail in order.SalesOrderDetail)
        {
            Console.WriteLine("  Product ID: {0} Unit Price {1}",
                orderDetail.ProductID, orderDetail.UnitPrice);
        }
    }
}

See Also

Concepts

Method-Based Query Syntax Examples (LINQ to Entities)