共用方式為


HOW TO:呼叫標準函式 (LINQ to Entities)

EntityFunctions 類別包含可公開標準函式以用於 LINQ to Entities 查詢的方法。 如需標準函式的詳細資訊,請參閱標準函式 (Entity SQL)

Dd456873.note(zh-tw,VS.100).gif注意:
EntityFunctions 類別中的 AsUnicodeAsNonUnicode 方法沒有對等的標準函式。

標準函式 (亦稱為彙總標準函式) 可直接叫用,它們會執行值集的計算,並傳回單一值。 呼叫的其他標準函式則做為 LINQ to Entities 查詢的一部份。 若要直接呼叫彙總函式,您必須將 ObjectQuery 傳遞至函式。 如需詳細資訊,請參閱下列第二個範例。

您可以在 LINQ to Entities 查詢中,使用 Common Language Runtime (CLR) 方法呼叫部分標準函式。 如需對應至標準函式的 CLR 方法清單,請參閱 CLR 方法與標準函式的對應

範例

下列範例使用 AdventureWorks 銷售模型。 範例執行 LINQ to Entities 查詢,使用 DiffDays 方法,傳回 SellEndDateSellStartDate 之間的差距小於 365 天的所有產品:

Using AWEntities As New AdventureWorksEntities()
    Dim products = From p In AWEntities.Products _
        Where EntityFunctions.DiffDays(p.SellEndDate, p.SellStartDate) < 365 _
        Select p

    For Each product In products
        Console.WriteLine(product.ProductID)
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    var products = from p in AWEntities.Products
                   where EntityFunctions.DiffDays(p.SellEndDate, p.SellStartDate) < 365
                   select p;
    foreach (var product in products)
    {
        Console.WriteLine(product.ProductID);
    }
}

下列範例使用 AdventureWorks 銷售模型。 範例直接呼叫彙總 StandardDeviation 方法,以傳回 SalesOrderHeader 小計的標準差。 請注意,ObjectQuery 已傳遞至函式,函式允許其接受呼叫時,不必成為 LINQ to Entities 查詢的一部份。

Using AWEntities As New AdventureWorksEntities()
    Dim stdDev As Double? = EntityFunctions.StandardDeviation( _
        From o In AWEntities.SalesOrderHeaders _
        Select o.SubTotal)

    Console.WriteLine(stdDev)
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    double? stdDev = EntityFunctions.StandardDeviation(
        from o in AWEntities.SalesOrderHeaders
        select o.SubTotal);

    Console.WriteLine(stdDev);
}

另請參閱

概念

呼叫 LINQ to Entities 查詢中的函式
LINQ to Entities 中的查詢