[转]Linq中GroupBy方法的使用总结
Demo模型类:
public class StudentScore
{ public int ID { set; get; } public string Name { set; get; } public string Course { set; get; } public int Score { set; get; } public string Term { set; get; } }
Demo示例代码:
static void Main()
{
var lst = new List<StudentScore>
{
new StudentScore {ID = , Name = "张三", Term = "第一学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "张三", Term = "第一学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "张三", Term = "第一学期", Course = "English", Score = },
new StudentScore {ID = , Name = "李四", Term = "第一学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "李四", Term = "第一学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "李四", Term = "第一学期", Course = "English", Score = },
new StudentScore {ID = , Name = "王五", Term = "第一学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "王五", Term = "第一学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "王五", Term = "第一学期", Course = "English", Score = },
new StudentScore {ID = , Name = "赵六", Term = "第一学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "赵六", Term = "第一学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "赵六", Term = "第一学期", Course = "English", Score = },
new StudentScore {ID = , Name = "张三", Term = "第二学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "张三", Term = "第二学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "张三", Term = "第二学期", Course = "English", Score = },
new StudentScore {ID = , Name = "李四", Term = "第二学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "李四", Term = "第二学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "李四", Term = "第二学期", Course = "English", Score = },
new StudentScore {ID = , Name = "王五", Term = "第二学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "王五", Term = "第二学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "王五", Term = "第二学期", Course = "English", Score = },
new StudentScore {ID = , Name = "赵六", Term = "第二学期", Course = "Math", Score = },
new StudentScore {ID = , Name = "赵六", Term = "第二学期", Course = "Chinese", Score = },
new StudentScore {ID = , Name = "赵六", Term = "第二学期", Course = "English", Score = },
};
//分组,根据姓名,统计Sum的分数,统计结果放在匿名对象中。两种写法。
//第一种写法
Console.WriteLine("---------第一种写法");
var studentSumScore_1 = (from l in lst
group l by l.Name
into grouped
orderby grouped.Sum(m => m.Score)
select new {Name = grouped.Key, Scores = grouped.Sum(m => m.Score)}).ToList();
foreach (var l in studentSumScore_1)
{
Console.WriteLine("{0}:总分{1}", l.Name, l.Scores);
} //第二种写法
Console.WriteLine("---------第二种写法");
var studentSumScore_2 = lst.GroupBy(m => m.Name)
.Select(k => new {Name = k.Key, Scores = k.Sum(l => l.Score)})
.OrderBy(m => m.Scores).ToList();
foreach (var l in studentSumScore_2)
{
Console.WriteLine("{0}:总分{1}", l.Name, l.Scores);
} //分组,根据2个条件学期和课程,统计各科均分,统计结果放在匿名对象中。两种写法。
Console.WriteLine("---------第一种写法");
var TermAvgScore_1 = (from l in lst
group l by new {l.Term, l.Course}
into grouped
orderby grouped.Average(m => m.Score) ascending
orderby grouped.Key.Term descending
select
new {grouped.Key.Term, grouped.Key.Course, Scores = grouped.Average(m => m.Score)})
.ToList();
foreach (var l in TermAvgScore_1)
{
Console.WriteLine("学期:{0},课程:{1},均分:{2}", l.Term, l.Course, l.Scores);
}
Console.WriteLine("---------第二种写法");
var TermAvgScore_2 = lst.GroupBy(m => new {m.Term, m.Course})
.Select(k => new {k.Key.Term, k.Key.Course, Scores = k.Average(m => m.Score)})
.OrderBy(l => l.Scores).ThenByDescending(l => l.Term);
foreach (var l in TermAvgScore_2)
{
Console.WriteLine("学期:{0},课程:{1},均分:{2}", l.Term, l.Course, l.Scores);
} //分组,带有Having的查询,查询均分>80的学生
Console.WriteLine("---------第一种写法");
var AvgScoreGreater80_1 = (from l in lst
group l by new {l.Name, l.Term}
into grouped
where grouped.Average(m => m.Score) >=
orderby grouped.Average(m => m.Score) descending
select
new
{
grouped.Key.Name,
grouped.Key.Term,
Scores = grouped.Average(m => m.Score)
}).ToList();
foreach (var l in AvgScoreGreater80_1)
{
Console.WriteLine("姓名:{0},学期:{1},均分:{2}", l.Name, l.Term, l.Scores);
}
Console.WriteLine("---------第二种写法");
/*此写法看起来较为复杂,第一个Groupby,由于是要对多个字段分组的,因此构建一个匿名对象,
对这个匿名对象分组,分组得到的其实是一个IEnumberable<IGrouping<匿名类型,StudentScore>>这样一个类型。
Where方法接受,和返回的都同样是IEnumberable<IGrouping<匿名类型,StudentScore>>类型,
其中Where方法签名Func委托的类型也就成了Func<IGrouping<匿名类型,StudentScore>,bool>,
之前说到,IGrouping<out TKey, out TElement>继承了IEnumerable<TElement>,
因此这种类型可以有Average,Sum等方法。 */
var AvgScoreGreater80_2 = lst.GroupBy(l => new {l.Name, l.Term})
.Where(m => m.Average(x => x.Score) >= )
.OrderByDescending(l => l.Average(x => x.Score))
.Select(l => new {l.Key.Name, l.Key.Term, Scores = l.Average(m => m.Score)}).ToList();
foreach (var l in AvgScoreGreater80_2)
{
Console.WriteLine("姓名:{0},学期:{1},均分:{2}", l.Name, l.Term, l.Scores);
} Console.ReadKey();
}
原文地址:http://hi.baidu.com/tewuapple/item/5e0e5a2862b67a8b9c63d103
[转]Linq中GroupBy方法的使用总结的更多相关文章
- 转载Linq中GroupBy方法的使用总结
Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...
- Linq中GroupBy方法的使用总结(转)
Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...
- Linq中GroupBy方法的使用总结(转载)
from:https://www.cnblogs.com/zhouzangood/articles/4565466.html Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均 ...
- 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)
转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...
- Linq 中 Distinct 方法扩展
原文链接 http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.html public static class LinqEx { publ ...
- linq 中执行方法
Database1Entities db = new Database1Entities(); protected void Page_Load(object sender, EventArgs e) ...
- 基础才是重中之重~理解linq中的groupby
linq将大部分SQL语句进行了封装,这使得它们更加面向对象了,对于开发者来说,这是一件好事,下面我从基础层面来说一下GroupBy在LINQ中的使用. 对GroupBy的多字段分组,可以看我的这篇文 ...
- C#3.0新增功能09 LINQ 基础07 LINQ 中的查询语法和方法语法
连载目录 [已更新最新开发文章,点击查看详细] 介绍性的语言集成查询 (LINQ) 文档中的大多数查询是使用 LINQ 声明性查询语法编写的.但是在编译代码时,查询语法必须转换为针对 .NET ...
- Linq 中按照多个值进行分组(GroupBy)
Linq 中按照多个值进行分组(GroupBy) .GroupBy(x => new { x.Age, x.Sex }) group emp by new { emp.Age, emp.Sex ...
随机推荐
- Java线程总结
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: class 类名 extends Thread ...
- Windows 2003/2008更改远程桌面端口脚本
保存为bat文件,点击运行按提示输入新端口自动完成,直接下载更改远程桌面端口脚本 @echo off color 0a title @@ 修改Windows XP/2003/2008远程桌面服务端 ...
- paper 72 :高动态范围(HDR)图像 HDR (High Dynamic Range)
In standard rendering, the red, green and blue values for a pixel are each represented by a fraction ...
- ORACLE CUP相关
遭遇cpu过多占用,表现为%usr很高,top 或者topas中cpu占用最多的进程为oracle server process. 则根据pid可以找出该pid对应的sql_text select s ...
- weka 文本分类(1)
一.初始化设置 1 jvm out of memory 解决方案: 在weka SimpleCLI窗口依次输入java -Xmx 1024m 2 修改配置文件,使其支持中文: 配置文件是在Weka安装 ...
- 写sql语句分别按日,星期,月,季度,年统计
--写sql语句分别按日,星期,月,季度,年统计销售额 --按日 ' group by day([date]) --按周quarter ' group by datename(week,[date]) ...
- 【NOIP模拟赛】工资
工资 [试题描述] 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可以自由安排这些时间,也就是说什么时候拿钱,老板说的不算,聪哥才有发言权!( ...
- Access数据库导入到SQL Server 2005 Express中
安装好SQL Server 2005 Express后,再安装SQL Server Management Studio Express CTP就可以很方便的使用控制台进行数据库的管理.但SQL Ser ...
- Java中的BufferedReader 的readLine方法
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java ...
- 中颖4位MCU的减法汇编指令
1, SUB M 执行动作: M - A -> A, 如果M-A的过程中没有产生借位,则CY= 1,如果产生了借位,则CY= 0. 其中,A为累加器. 2, SBI M, I 执行动作:M - ...