Silentdoer

导航

GroupBy(..)的四种声明方式的理解及调用

这里我们以 List<Student> studs作为 source,但是注意,studs中的学生可以是分别属于不同的班级和年级

先看GroupBy的第一种声明:

public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);

// IGrouping<TKey,TSource> 对应的类的声明可以粗糙的认为:class Gro { TKey key;List<TSource> list;} 且Gro实现了IEnumerable<TSource>; 对 Gro进行foreach
// 时,循环访问的是 其内部的 list的元素。
// 这种声明的调用方式最简单 studs.GroupBy(stu=>stu.ClassId); // 即以 ClassId作为分组的Key,返回的是类似 Lookup对象

第二种声明:

public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector);  // 一般而言这种是用来返回 {Key,Count,TotalXX,Average}

// 第一个委托是选出 key值,即stu.ClassId,去重后 将每个key值 赋给第二个委托的 key变量,将该key值对应的 n个IEnumerable<TSource>赋给listForKey,并
// 由 key和listForKey 进行增减或平均或总计,返回 TResult对象;本函数返回的 IEnumerable<TResult> 集合listResult,listResult的元素个数等于分组的组数
// 调用方式为:studs.GroupBy(stu=>stu.ClassId,(key,listForKey)=>new{ClassId=key,Count=listForKey.Count(),/*如果有身高属性还可以有总身高等等*/});
// 第二个委托中有 TKey参数一般是为了方便,其实不用也可以,如: new{ClassId=listForKey[0].ClassId,......},但是,这样自然是不美观的。。

第三种声明:

public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector);

// 这个其实和第一种声明实现的 功能差不多,第一个声明中 IGrouping内的集合的元素类型默认就是 进行分组的数据表或集合中元素的类型,这里就是将该类型进行了自定义
// 调用形式为:studs.GroupBy(stu=>stu.ClassId,stud=>new{ClassId=stud.ClassId,......}); // stu和stud实际上可以一样,因为它们在GroupBy属于不同的层

第四种声明:

public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector);

// 这个就是声明二和声明三进行整合得到的新的 GroupBy(...); 我个人是觉得没太大用,因为TElement是由TSource衍生出来的,直接用声明二就好了??
// 除非是TElement是对TSource进行了扩展,这时候或许有一些用。。。

 

posted on 2015-08-31 15:48  Silentdoer  阅读(460)  评论(0编辑  收藏  举报