共用方式為


ObjectQuery<T>.GroupBy(String, String, ObjectParameter[]) 方法

定義

依據指定的準則,分組查詢結果。

public:
 System::Data::Objects::ObjectQuery<System::Data::Common::DbDataRecord ^> ^ GroupBy(System::String ^ keys, System::String ^ projection, ... cli::array <System::Data::Objects::ObjectParameter ^> ^ parameters);
public System.Data.Objects.ObjectQuery<System.Data.Common.DbDataRecord> GroupBy (string keys, string projection, params System.Data.Objects.ObjectParameter[] parameters);
member this.GroupBy : string * string * System.Data.Objects.ObjectParameter[] -> System.Data.Objects.ObjectQuery<System.Data.Common.DbDataRecord>
Public Function GroupBy (keys As String, projection As String, ParamArray parameters As ObjectParameter()) As ObjectQuery(Of DbDataRecord)

參數

keys
String

分組結果所依據的索引鍵資料行。

projection
String

定義投影之選取屬性的清單。

parameters
ObjectParameter[]

這個方法所使用的零個或多個參數。

傳回

屬於 ObjectQuery<T> 型別的新 DbDataRecord 執行個體,它就相當於套用了 GROUP BY 的原始執行個體。

例外狀況

query 參數是 null 或空字串。

-或-

projection 參數是 null 或空字串。

範例

這個範例會建立新的 ObjectQuery<T> 物件,其中包含依據產品名稱分組現有查詢的結果。

    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        string queryString = @"SELECT VALUE product
            FROM AdventureWorksEntities.Products AS product";

        ObjectQuery<Product> productQuery =
            new ObjectQuery<Product>(queryString,
                context, MergeOption.NoTracking);

        ObjectQuery<DbDataRecord> productQuery2 =
            productQuery.GroupBy("it.name AS pn",
            "Sqlserver.COUNT(it.Name) as count, pn");

        // Iterate through the collection of Products
        // after the GroupBy method was called.
        foreach (DbDataRecord result in productQuery2)
        {
            Console.WriteLine("Name: {0}; Count: {1}",
                result["pn"], result["count"]);
        }
    }
}

這個範例會傳回一組巢狀數據記錄,其中包含 Contact.LastName 數據行,並以字母順序依 的第一個字母 Contact.LastName來分組和排序。

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define the query with a GROUP BY clause that returns
    // a set of nested LastName records grouped by first letter.
    ObjectQuery<DbDataRecord> query =
        context.Contacts
        .GroupBy("SUBSTRING(it.LastName, 1, 1) AS ln", "ln")
        .Select("it.ln AS ln, (SELECT c1.LastName " +
        "FROM AdventureWorksEntities.Contacts AS c1 " +
        "WHERE SubString(c1.LastName, 1, 1) = it.ln) AS CONTACT")
        .OrderBy("it.ln");

    // Execute the query and walk through the nested records.
    foreach (DbDataRecord rec in
        query.Execute(MergeOption.AppendOnly))
    {
        Console.WriteLine("Last names that start with the letter '{0}':",
                    rec[0]);
        List<DbDataRecord> list = rec[1] as List<DbDataRecord>;
        foreach (DbDataRecord r in list)
        {
            for (int i = 0; i < r.FieldCount; i++)
            {
                Console.WriteLine("   {0} ", r[i]);
            }
        }
    }
}

備註

GroupBy 會套用 參數所 projection 指定的投影。 這表示,ObjectQuery<T> 方法所傳回的 GroupBy 一律屬於 DbDataRecord 型別。 如需詳細資訊,請參閱物件查詢

適用於

另請參閱