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。 有关详细信息,请参阅对象查询

适用于

另请参阅