[转]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 ...
随机推荐
- C# Enum Type
class Program { public enum TimeOfDay { Morining, Afternoon, Evening } static void Main(string[] arg ...
- C#控件:TabControl
tabcontrol在切换页面的时候经常用到 这里讲下如何让tabcontrol更好看 ref:http://blog.csdn.net/conmajia/article/details/759671 ...
- Array.prototype.each
Array.prototype.each = function(closure){ //递归合并 return this.length ? [closure(this.slice(0,1))].con ...
- POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)
Description Given a big integer number, you are required to find out whether it's a prime number. In ...
- windows系统调用 线程创建
#include "windows.h" #include "iostream" using namespace std; class CWorkerThrea ...
- vs2010的快捷键
vs2010的快捷键 VS2008快捷键大全 Ctrl+m+Crtr+o折叠所有大纲Ctrl+M+Crtr+P: 停止大纲显示Ctrl+K+Crtr+C: 注释选定内容Ctrl+K+Crtr+U: 取 ...
- 夺命雷公狗---node.js---10之POST的接收
首先我们在项目下创建一个表单,代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SelectObj
zw版[转发·台湾nvp系列Delphi例程]HALCON SelectObj procedure TForm1.Button1Click(Sender: TObject);var img : HIm ...
- 【转】SQL Server T-SQL高级查询
SQL Server T-SQL高级查询 高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student; //查询student ...
- UI优化
进入正题,我们这一篇文章会提到为什么使用HierarchyViewer,怎么使用HierarchyViewer,后者内容会多一下. 为什么使用HierarchyViewer 不合理的布局会使我们的应用 ...