LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
| Grouping Operators | Description |
|---|---|
| GroupBy | GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 |
| ToLookup | ToLookup is the same as GroupBy; the only difference is the execution of GroupBy is deferred whereas ToLookup execution is immediate. |

IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Abram" , Age = }
};
var groupedResult = from s in studentList
group s by s.Age;
//iterate each group
foreach (var ageGroup in groupedResult)
{
Console.WriteLine("Age Group: {0}", ageGroup .Key); //Each group has a key
foreach(Student s in ageGroup) // Each group has inner collection
Console.WriteLine("Student Name: {0}", s.StudentName);
}
可以使用foreach遍历group,每个Group包含一个key和内部的集合
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Abram" , Age = }
};
var groupedResult = studentList.GroupBy(s => s.Age);
foreach (var ageGroup in groupedResult)
{
Console.WriteLine("Age Group: {0}", ageGroup.Key); //Each group has a key
foreach(Student s in ageGroup) //Each group has a inner collection
Console.WriteLine("Student Name: {0}", s.StudentName);
}
ToLookup和GroupBy一样,唯一不同的是GroupBy是延迟执行,而ToLookup是立即执行
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Abram" , Age = }
};
var lookupResult = studentList.ToLookup(s => s.age);
foreach (var group in lookupResult)
{
Console.WriteLine("Age Group: {0}", group.Key); //Each group has a key
foreach(Student s in group) //Each group has a inner collection
Console.WriteLine("Student Name: {0}", s.StudentName);
}
注意:GroupBy和ToLookup返回一个集合(包含key,根据key分组的内部集合)
LINQ 学习路程 -- 查询操作 GroupBy ToLookUp的更多相关文章
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Conversion Operators
Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Conve ...
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
随机推荐
- vux报错 this指针问题
报错: 代码: 结果:取不到this.tishi,不明白为什么在请求内部会取不到这个值. 分析:this可能有问题 解决: 结果: 这样就能取到this.tishi的值了. 总结: methods:{ ...
- (分解质因数模板)求 1~r 内与 n 互素的元素个数
void Solve(LL n){ ///分解质因数保存结果于p p.clear(); ; i*i<=n; i++) ){ p.push_back(i); ) n/=i; } ) p.push_ ...
- 【BZOJ2217】[Poi2011]Lollipop 乱搞
[BZOJ2217][Poi2011]Lollipop Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T& ...
- {Repeater控件} Repeater控件的用法流程及实例
一.Repeater控件的用法流程及实例: 1.首先建立一个网站,新建一个网页index.aspx. 2.添加或者建立APP_Data数据文件,然后将用到的数据库文件放到APP_Data文件夹中. 3 ...
- thinkphp --- 写入日志
在开发过程中,对于一些参数,不好直接输入或者打印调试,特别是在微信开发过程中,这个时候,通过日志来查看信息就显得格外重要. 下面是在TP3.2.3框架中,写入日志的方法: public functio ...
- [Gradle] 查看项目依赖
查看项目的编译依赖 $ ./gradlew :app:dependencies --configuration compile 查看具体某个库的依赖 $ ./gradlew -q :app:depen ...
- 170718、springboot编程之发送邮件
Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用Jav ...
- 用到了yii2 hasMany() 方法,一对多关联
view页面代码:其中supply,item,price是一个AR类都是一个类,item和prices是一对多关系: [ 'label' => '参考', 'format' => 'htm ...
- Code Forces 652A Gabriel and Caterpillar
A. Gabriel and Caterpillar time limit per test 1 second memory limit per test 256 megabytes input st ...
- nginx详解之语法规则
1.location [=|~|~*|^~] /uri/ { … } location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] ...