根据指定的键选择器函数对序列中的元素进行分组。

命名空间:  System.Linq
程序集:  System.Core(在 System.Core.dll 中)

 
public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
this IQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector
)

类型参数

TSource

source 中的元素的类型。

TKey

由 keySelector 表示的函数返回的键类型。

参数

source
类型:System.Linq.IQueryable<TSource>
要对其元素进行分组的 IQueryable<T>
keySelector
类型:System.Linq.Expressions.Expression<Func<TSource, TKey>>
用于提取每个元素的键的函数。

返回值

类型:System.Linq.IQueryable<IGrouping<TKey, TSource>>
在 C# 中为 IQueryable<IGrouping<TKey, TSource>>,或者在 Visual Basic 中为 IQueryable(Of IGrouping(Of TKey, TSource)),其中每个 IGrouping<TKey, TElement> 对象都包含一个对象序列和一个键。

使用说明

在 Visual Basic 和 C# 中,可以在 IQueryable<TSource> 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见扩展方法 (Visual Basic)扩展方法(C# 编程指南)

异常 条件
ArgumentNullException

source 或 keySelector 为 null。

此方法至少有一个 Expression<TDelegate> 类型的实参,其形参类型是 Func<T, TResult> 类型之一。 对于这些参数,您可以 lambda 表达式形式传递,并且该表达式将被编译为 Expression<TDelegate>

GroupBy<TSource, TKey>(IQueryable<TSource>, Expression<Func<TSource, TKey>>) 方法生成 MethodCallExpression,表示调用 GroupBy<TSource, TKey>(IQueryable<TSource>, Expression<Func<TSource, TKey>>) 自身作为构造的泛型方法。 然后,它将 MethodCallExpression 传递给 IQueryProvider 的 CreateQuery<TElement>(Expression) 方法,由 source 参数的 Provider 属性表示。

因执行表示调用 GroupBy<TSource, TKey>(IQueryable<TSource>, Expression<Func<TSource, TKey>>) 的表达式目录树而发生的查询行为取决于 source 参数类型的实现。 预期的行为是根据键值对 source 中的元素进行分组,而键值是通过对每个元素调用 keySelector 获得的。

下面的代码示例演示如何使用 GroupBy<TSource, TKey>(IQueryable<TSource>, Expression<Func<TSource, TKey>>) 对序列中的元素进行分组。

 
            class Pet
{
public string Name { get; set; }
public int Age { get; set; }
} public static void GroupByEx1()
{
// Create a list of Pet objects.
List<Pet> pets =
new List<Pet>{ new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 },
new Pet { Name="Daisy", Age=4 } }; // Group the pets using Pet.Age as the key.
// Use Pet.Name as the value for each entry.
var query = pets.AsQueryable().GroupBy(pet => pet.Age); // Iterate over each IGrouping in the collection.
foreach (var ageGroup in query)
{
Console.WriteLine("Age group: {0} Number of pets: {1}", ageGroup.Key, ageGroup.Count());
}
} /*
This code produces the following output: Age group: 8 Number of pets: 1
Age group: 4 Number of pets: 2
Age group: 1 Number of pets: 1 */

.NET Framework

受以下版本支持:4.5、4、3.5

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

可移植类库

受以下版本支持:可移植类库

适用于 Windows 应用商店应用的 .NET

受以下版本支持:Windows 8

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并不是对每个平台的所有版本都提供支持。

最新文章

  1. PHP开发环境搭建
  2. ps切图设置
  3. maven引入jar包时,一个jar的引入错误,会导致后来的jar包的引入。
  4. linux包之gdb之gdb命令与core文件产生
  5. python写的battle ship小游戏 - 1.0
  6. 实现Runnable接口和扩展Thread使用场景
  7. iOS开发 关于addChildViewController的理解
  8. ASP.NET Core 一步步搭建个人网站(持续更新中~~~)
  9. Qt--自定义Delegate
  10. 使用Jmeter监测服务器性能指标
  11. JSON for-in 遍历
  12. LitJson的用法
  13. thinkphp每次跳转时都会显示笑脸的修改
  14. tail 命令(转)
  15. 20. Valid Parentheses(括号匹配,用桟)
  16. jQuery 遍历 - children() 方法 获取指定id下子元素的值
  17. Robot Framework接口测试(4)
  18. 类模版的static成员
  19. CodeVs1515 跳
  20. Hadoop- MR的shuffle过程

热门文章

  1. commonJS — 日期操作(for Date)
  2. 深入理解JVM虚拟机-2自动内存管理机制
  3. ajax的详细学习
  4. IoC 之 2.1 IoC基础(壹)
  5. 20145218 《Java程序设计》第四周学习总结
  6. WLAN频段介绍-04
  7. [redis] 普通 RedisPool 的 CRUD 实现
  8. 终端执行python shell的方法
  9. JavaWeb基础: Web应用和Web服务器
  10. addViewController之后view里面的点击事件不响应