[转]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 ...
随机推荐
- 变形--矩阵 matrix()
matrix() 是一个含六个值的(a,b,c,d,e,f)变换矩阵,用来指定一个2D变换,相当于直接应用一个[a b c d e f]变换矩阵.就是基于水平方向(X轴)和垂直方向(Y轴)重新定位元素 ...
- 实现listview的条目点击后改变背景颜色
gv_categoryeffect_gridview.setChoiceMode(GridView.CHOICE_MODE_SINGLE);,再设置一个selector的背景选择器 getResour ...
- redis连接数问题
redis连接数查看 info client redis连接数满了,不会继续建立连接. 造成redis连接数满了原因有很多. 1.建立新连接不close()的话redis连接不会回归连接池. 显示所有 ...
- 13.熟悉JDK的配置,环境变量
已经做烂的东西,公司的新人环境配置手册文档Java方面的就是我写的,有意的留邮箱,很详细
- 夺命雷公狗---Thinkphp----12之文章的增删改查(图片上传和关联查询)
我们由于表分析的不够完善,所以我们来加多一个tid的字段,到时候主要目的是为了更好的遍历出文章是属于那个分类下的,表如下所示: 那么下一步我们就开始创建一个ArticleController.clas ...
- 深入了解webservice_开发实战篇
服务器端: 第一: 配置环境 创建一个Web Project,将XFire自己的包及其XFire依赖的jar包(6个jar包)全部导入到classpath下,方法是,只需将jar包粘贴在WebRoot ...
- 【ipython技巧】使用shell命令
在ipython终端时,可能临时需要使用shell命令进行简单处理: 可以在shell命令前面使用 !(感叹号) 比如在win7,ipython下想要使用sublime新建一个py,可以这样 !sub ...
- Yii增删改查操作
增: 1 第一种 $post=new Post; $post->title='sample post'; $post->content='content for the sample po ...
- viewpager viewpager+fragment
内页面 不单写页面 viewpager+布局 import java.util.ArrayList; import java.util.List; import android.os.Bundl ...
- UINavigationController详解一(转)UIBarButtonItem
本文出自:http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html 特别感谢. 1.UINavigationControlle ...