共用方式為


CLR 方法呼叫和函式對應

Entity Framework 提供一組標準函式,可實作在許多資料庫系統常見的功能,例如字串操作和數學函式。這樣就可以讓開發人員以廣泛的資料庫系統為相標。從某種查詢技術 (例如 LINQ to Entities) 呼叫時,這些標準函式會轉譯成所使用之提供者的正確對應存放函式。這樣就可以利用跨資料來源的通用形式來表示函式引動過程,在不同的資料來源提供一致的查詢體驗。如果運算元為數值型別,位元運算 AND、OR,、NOT 和 XOR 運算子也是對應到這些標準函式。對於布林運算元,位元運算 AND、OR、NOT 和 XOR 運算子會計算這些運算元的邏輯 AND、OR、NOT 和 XOR 運算。如需詳細資訊,請參閱標準函式 (Entity SQL)

就 LINQ 案例而言,針對 Entity Framework 的查詢會透過標準函式將某些 CLR 方法對應到基礎資料來源。在 LINQ to Entities 查詢中,不是明確對應到標準函式的任何方法呼叫,將會導致擲回執行階段 NotSupportedException 例外狀況 (Exception)。如需對應到標準函式的 CLR 方法清單,請參閱 CLR 方法與標準函式的對應

以下範例是查詢位於 Algiers Drive 的地址。查詢中的 Contains 方法呼叫是對應到 IndexOf 標準函式。

Using AWEntities As New AdventureWorksEntities()
    Dim addresses As ObjectQuery(Of Address) = AWEntities.Address

    Dim query = _
    From address In addresses _
    Where address.AddressLine1.Contains("Algiers Dr.") _
    Select address

    For Each algiersAddress As Address In query
        Console.WriteLine("Address 1: " + algiersAddress.AddressLine1)
    Next

End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<Address> addresses = AWEntities.Address;

    IQueryable<Address> query = from address in addresses
                                where address.AddressLine1.Contains("Algiers Dr.")
                                select address;

    // Addresses on Algiers Dr.
    foreach (Address algiersAddress in query)
    {
        Console.WriteLine("Address 1: " + algiersAddress.AddressLine1);
    }
}

另請參閱

概念

標準函式 (Entity SQL)

其他資源

用 LINQ to Entities 查詢