共用方式為


常數運算式

常數運算式是由常數值所組成。常數值會直接轉換成常數命令樹運算式,而不需在用戶端上進行任何轉譯。其中包括產生常數值的運算式。因此,所有牽涉到常數的運算式應該都會有資料來源行為。這樣可能會產生與 CLR 行為不同的行為。

下列範例會顯示在伺服器上評估的常數運算式。

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

另請參閱

概念

LINQ to Entities 查詢中的運算式