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

命名空间:  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 并不是对每个平台的所有版本都提供支持。

Queryable.GroupBy<TSource, TKey> 方法 (IQueryable<TSource>, Expression<Func<TSource, TKey>>) 转的更多相关文章

  1. EF Core 封装方法Expression<Func<TObject, bool>>与Func<TObject, bool>区别

    unc<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后就 ...

  2. Expression<Func<T, bool>>与Func<T, bool>的区别

    转自:http://www.cnblogs.com/wow-xc/articles/4952233.html Func<TObject, bool>是委托(delegate) Expres ...

  3. Expression<Func<TObject, bool>>与Func<TObject, bool>的区别

    Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...

  4. Expression<Func<T>>和Func<T>

    以前用EF的时候,由于where的时候有Expression<Func<T>>和Func<T>两种查询条件,误用了Func<T>那个重载,后来还想通过f ...

  5. 实现最小宽度的几种方法及CSS Expression[转]

    实现最小宽度的几种方法及CSS Expression[转] 实现最小宽度的几种方法:css表达式尽量不用 支持FF IE7  IE6 .test { background:blue; min-widt ...

  6. Linq分组操作之GroupBy,GroupJoin扩展方法源码分析

    Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...

  7. Entity FrameWork 中使用Expression<Func<T,true>>访问数据库性能优化

    问题的本质是:扩展的Where方法有四个参数重载.传进去Func<T,true>那么返回值是IEnumable的接口类型的集合,如果是Expression<Func<T,tru ...

  8. Expression<Func<T,TResult>>和Func<T,TResult>

    1.Expression<Func<T,TResult>>是表达式 //使用LambdaExpression构建表达式树 Expression<Func<int, ...

  9. .NetCore 扩展封装 Expression<Func<T, bool>> 查询条件遇到的问题

    前面的文章封装了查询条件 自己去组装条件,但是对 And  Or  这种组合支持很差,但是也不是不能支持,只是要写更多的代码看起来很臃肿 根据 Where(Expression<Func< ...

随机推荐

  1. JavaScript学习笔记(十二) 回调模式(Callback Pattern)

    函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...

  2. Git分支管理详解

    内容来源:http://blog.jobbole.com/25775/ Git简介 Git是分布式版本控制工具:Git 收取的是项目历史的所有数据(每一个文件的每一个版本),服务器上有的数据克隆之后本 ...

  3. sleep函数

    Linux下: #include <unistd.h> sleep(1); // 睡眠1秒 usleep(1); // 睡眠1微妙

  4. web基础之hibernate(一篇)

    hibernate的一些基本的认识 1.       hibenate是一个框架(framework) 2.       hibernate是一个orm框架 3.       orm(object r ...

  5. 张艾迪(创始人):DCM的不识人.我说我会像乔布斯一样成为投资者的骄傲

      Eidyzhang解码:天才Eidyzhang的诞生 张艾迪(创始人):第一个实习生精英团队 张艾迪(创始人):DCM的不识人.我说我会像乔布斯一样成为投资者的骄傲 2014-05-31 09:4 ...

  6. spring关于urlpattern

    视图解析器(ViewResolver)注册中央调度器定制处理器jsp页面搭建springmvc.xml配置效果图第一个案例提升----视图解析器关于urlpattern说法最好配成*.do 不能配成/ ...

  7. js创建table表格

    //js创建table表格var tr;var cell;for(var i=0;i<10;i++){ //创建一个tr tr=document.createElement('tr'); doc ...

  8. AngularJS directive入门例子

    这是<AngularJS>这本书里面提供的一个例子: JS代码: var expanderModule=angular.module('expanderModule', []) expan ...

  9. 让DIV实现抖动效果!

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  10. 用while循环语句计算1!+2!+……20!之和

    package nothh; public class mmm { public static void main(String[] args) { // TODO Auto-generated me ...