共用方式為


HOW TO:彙總資料 (Entity Framework)

彙總運算會從值集合計算出單一值。匯總運算的一個範例是,當您使用一個月中每天的溫度值來計算每天平均溫度時。本主題顯示如何依連絡人識別碼分組訂單,以及如何取得每個連絡人識別碼的平均總金額。

相同範例會使用下列每個 實體架構 查詢技術顯示:

  • LINQ to Entities

  • 使用 ObjectQuery<T> 的 Entity SQL

本主題的範例是根據 Adventure Works Sales Model。若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。您也可以使用 [Entity Data Model 精靈] 定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:使用 Entity Data Model 精靈 (Entity Framework)

範例

以下是 LINQ 到實體 範例。

Using AWEntities As New AdventureWorksEntities
    Dim orders As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader

    Dim query = _
        From ord In orders _
        Let contID = ord.Contact.ContactID _
        Group ord By contID Into g = Group _
        Select New With _
        { _
            .Category = contID, _
            .averageTotalDue = _
                g.Average(Function(ord) ord.TotalDue) _
         }

    For Each ord In query
        Console.WriteLine("ContactID = {0} " & vbTab & _
            " Average TotalDue = {1}", _
            ord.Category, ord.averageTotalDue)
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;

    var query =
        from order in orders
        group order by order.Contact.ContactID into g
        select new
        {
            Category = g.Key,
            averageTotalDue = g.Average(order => order.TotalDue)
        };

    foreach (var order in query)
    {
        Console.WriteLine("ContactID = {0} \t Average TotalDue = {1}",
            order.Category, order.averageTotalDue);
    }
}

以下是 實體 SQL 範例。

Using advWorksContext As AdventureWorksEntities = New AdventureWorksEntities
    Dim esqlQuery As String = "SELECT contactID, AVG(order.TotalDue) " & _
                                    " FROM(AdventureWorksEntities.SalesOrderHeader)" & _
                                    " AS order GROUP BY order.Contact.ContactID as contactID"

    Try
        Dim rec As DbDataRecord
        Dim query As New ObjectQuery(Of DbDataRecord)(esqlQuery, advWorksContext)
        For Each rec In query
            Console.WriteLine("ContactID = {0}  Average TotalDue = {1} ", _
                    rec.Item(0), rec.Item(1))
        Next
    Catch ex As EntityException
        Console.WriteLine(ex.ToString())
    Catch ex As InvalidOperationException
        Console.WriteLine(ex.ToString())
    End Try

End Using
using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    string esqlQuery = @"SELECT contactID, AVG(order.TotalDue) 
                            FROM AdventureWorksEntities.SalesOrderHeader 
                            AS order GROUP BY order.Contact.ContactID as contactID";

    try
    {
        foreach (DbDataRecord rec in
            new ObjectQuery<DbDataRecord>(esqlQuery, advWorksContext))
        {
            Console.WriteLine("ContactID = {0}  Average TotalDue = {1} ",
                rec[0], rec[1]);
        }
    }
    catch (EntityException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

另請參閱

工作

HOW TO:群組資料 (Entity Framework)

其他資源

查詢 Entity Data Model (Entity Framework 工作)