共用方式為


比較運算式

比較運算式會檢查常數值、屬性值或方法結果是否等於、不等於、大於或小於另一個值。如果特定比較對於 LINQ to Entities 無效,將會擲回例外狀況。所有的比較 (隱含和明確) 都會要求所有元件在資料來源內都是可以比較的。Where 子句中經常會使用比較運算式來限制查詢結果。

下列查詢運算式語法範例顯示一個查詢,此查詢會傳回銷售訂單號碼等於 "SO43663" 的結果:

Dim salesOrderNumber = "SO43663"
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.SalesOrderNumber = salesOrderNumber _
        Select s

    Console.WriteLine("Sales info-")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Ship date: " & sale.ShipDate)
    Next
End Using
string salesOrderNumber = "SO43663";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.SalesOrderNumber == salesOrderNumber
        select s;

    Console.WriteLine("Sales info-");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Ship date: " + sale.ShipDate);
    }
}

下列以方法為基礎的查詢語法範例顯示一個查詢,此查詢會傳回銷售訂單號碼等於 "SO43663" 的結果:

Dim salesOrderNumber = "SO43663"
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        context.SalesOrderHeaders _
            .Where(Function(s) s.SalesOrderNumber = salesOrderNumber) _
            .Select(Function(s) s)

    Console.WriteLine("Sales info-")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Ship date: " & sale.ShipDate)
    Next
End Using
    string salesOrderNumber = "SO43663";
    IQueryable<SalesOrderHeader> salesInfo =
        context.SalesOrderHeaders
        .Where(s => s.SalesOrderNumber == salesOrderNumber)
        .Select(s => s);

    Console.WriteLine("Sales info-");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Ship date: " + sale.ShipDate);
    }
}

下列查詢運算式語法範例顯示一個查詢,此查詢會傳回出貨日期等於 2001 七月 8 日的銷售訂單資訊:

Using context As New AdventureWorksEntities()
    Dim dt As DateTime = New DateTime(2001, 7, 8)
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.ShipDate = dt _
        Select s

    Console.WriteLine("Orders shipped on August 7, 2001:")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine()
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    DateTime dt = new DateTime(2001, 7, 8);

    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.ShipDate == dt
        select s;

    Console.WriteLine("Orders shipped on August 7, 2001:");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine();
    }
}

下列以方法為基礎的查詢語法範例顯示一個查詢,此查詢會傳回出貨日期等於 2001 七月 8 日的銷售訂單資訊:

Using context As New AdventureWorksEntities()
    Dim dt As DateTime = New DateTime(2001, 7, 8)

    Dim salesInfo = _
        context.SalesOrderHeaders _
        .Where(Function(s) s.ShipDate = dt) _
        .Select(Function(s) s)

    Console.WriteLine("Orders shipped on August 7, 2001:")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine()
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    DateTime dt = new DateTime(2001, 7, 8);

    IQueryable<SalesOrderHeader> salesInfo =
        context.SalesOrderHeaders
        .Where(s => s.ShipDate == dt)
        .Select(s => s);

    Console.WriteLine("Orders shipped on August 7, 2001:");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine();
    }
}

產生常數的運算式會在伺服器上轉換,而且不會嘗試執行本機評估。以下列範例在 Where 子句中使用了產生常數的運算式。

Dim totalDue = 200 + 3
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.TotalDue >= totalDue _
        Select s.SalesOrderNumber

    Console.WriteLine("Sales order numbers:")
    For Each orderNum As String In salesInfo
        Console.WriteLine(orderNum)
    Next
End Using
Decimal totalDue = 200 + 3;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<string> salesInfo =
        from s in context.SalesOrderHeaders
        where s.TotalDue >= totalDue
        select s.SalesOrderNumber;

    Console.WriteLine("Sales order numbers:");
    foreach (string orderNum in salesInfo)
    {
        Console.WriteLine(orderNum);
    }
}

LINQ to Entities 不支援使用使用者類別當做常數。但是,使用者類別上的屬性參考會視為常數,而且將會轉換成命令樹常數運算式,並在資料來源上執行。

Class AClass
    Public ID As Integer
End Class
class AClass { public int ID;}
Using context As New AdventureWorksEntities()
    Dim aClass As AClass = New aClass()
    aClass.ID = 43663

    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.SalesOrderID = aClass.ID _
        Select s

    Console.WriteLine("Order info-")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales order number: " & sale.SalesOrderNumber)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine()
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    AClass aClass = new AClass();
    aClass.ID = 43663;

    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.SalesOrderID == aClass.ID
        select s;

    Console.WriteLine("Order info-");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales order number: " + sale.SalesOrderNumber);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine();
    }
}

不支援傳回常數運算式的方法。下列範例包含 Where 子句中傳回常數的方法。此範例將會在執行階段擲回例外狀況。

Using context As New AdventureWorksEntities()
    Dim aClass2 As AClass2 = New aClass2()

    ' Throws a NotSupportedException.
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.SalesOrderID = aClass2.returnInt() _
        Select s

    Console.WriteLine("Order info-")
    Try
        For Each sale As SalesOrderHeader In salesInfo
            Console.WriteLine("Sales order number: " & sale.SalesOrderNumber)
            Console.WriteLine("Total due: " & sale.TotalDue)
            Console.WriteLine()
        Next
    Catch ex As NotSupportedException
        Console.WriteLine("Exception: {0}", ex.Message)
    End Try
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    MyClass2 myClass = new MyClass2();


    //Throws a NotSupportedException
    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.SalesOrderID == myClass.returnInt()
        select s;

    Console.WriteLine("Order info-");
    try
    {
        foreach (SalesOrderHeader sale in salesInfo)
        {
            Console.WriteLine("Sales order number: " + sale.SalesOrderNumber);
            Console.WriteLine("Total due: " + sale.TotalDue);
            Console.WriteLine();
        }
    }
    catch (NotSupportedException ex)
    {
        Console.WriteLine("Exception: {0}", ex.Message);
    }
}

另請參閱

概念

LINQ to Entities 查詢中的運算式