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方法的使用总结的更多相关文章

  1. 转载Linq中GroupBy方法的使用总结

    Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...

  2. Linq中GroupBy方法的使用总结(转)

    Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...

  3. Linq中GroupBy方法的使用总结(转载)

    from:https://www.cnblogs.com/zhouzangood/articles/4565466.html Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均 ...

  4. 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)

    转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...

  5. Linq 中 Distinct 方法扩展

    原文链接 http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.html public static class LinqEx { publ ...

  6. linq 中执行方法

    Database1Entities db = new Database1Entities(); protected void Page_Load(object sender, EventArgs e) ...

  7. 基础才是重中之重~理解linq中的groupby

    linq将大部分SQL语句进行了封装,这使得它们更加面向对象了,对于开发者来说,这是一件好事,下面我从基础层面来说一下GroupBy在LINQ中的使用. 对GroupBy的多字段分组,可以看我的这篇文 ...

  8. C#3.0新增功能09 LINQ 基础07 LINQ 中的查询语法和方法语法

    连载目录    [已更新最新开发文章,点击查看详细] 介绍性的语言集成查询 (LINQ) 文档中的大多数查询是使用 LINQ 声明性查询语法编写的.但是在编译代码时,查询语法必须转换为针对 .NET ...

  9. Linq 中按照多个值进行分组(GroupBy)

    Linq 中按照多个值进行分组(GroupBy) .GroupBy(x => new { x.Age, x.Sex }) group emp by new { emp.Age, emp.Sex ...

随机推荐

  1. BJFU 1009

    描述 现在社会上的抽奖活动简直是太多了.前段时间中国联通就举办了一个很无聊的抽奖活动,规则是每人可以向中国联通的短信系统发送一个实数,系统每天会从这些数字中选择一个无重复(就是有且只有一个)且最小的数 ...

  2. BaseAction的一般写法

    package com.mi.action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import ja ...

  3. 夺命雷公狗---DEDECMS----22dedecms让A标签进入对应的内容页

    我们的模版里的超链接都是写死的,这都是不符合实际网站的需求的,我们要将他让他边活的,而并非死的.. 我们首先要将前端给我们的内容页面的模版放到目标目录里面,但是我们的内容页的模版名叫啥呢?我们可以来查 ...

  4. java 操作excel 文件

    JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...

  5. 《zw版·Halcon-delphi系列原创教程》 邮票艺术品自动分类脚本

    <zw版·Halcon-delphi系列原创教程> 邮票艺术品自动分类脚本 邮票艺术品自动分类脚本,是个综合应用,有不同尺寸图像的自动识别.区域分割 还有作品附近文字的自动分割 此类项目, ...

  6. android 项目学习随笔十六( 广告轮播条播放)

    广告轮播条播放 if (mHandler == null) {//在此初始化mHandler , 保证消息不重复发送 mHandler = new Handler() { public void ha ...

  7. 如何清除DNS缓存,使用cmd命令清理DNS缓存方法

    如何清除DNS缓存,使用cmd命令清理DNS缓存方法 有时候电脑突然上不了网,或者存在某些网站打不开的情况,但别的网站又可以打开,解决办法需要清除DNS缓存,那么如何清除DNS缓存呢,最常用的方法就是 ...

  8. asp显示记录条数

    Sql = "select * from xin126 where ID=" & id Rs.Open Sql,Conn,1,1 %> 共有<strong st ...

  9. android 常用命令

    1.查看当前手机界面的 Activity   dumpsys | grep "mFocusedActivity" 查看任务栈 dumpsys | grep "Hist&q ...

  10. KindEditor图片批量上传

    KindEditor编辑器图片批量上传采用了上传插件swfupload.swf,所以后台上传文件方法返回格式应为JSONObject的String格式(注). JSONObject格式: JSONOb ...