linqgroupbyHaving语句的写法.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
groupby分组18.
19.//分组,根据2个条件学期和课程,统计各科均分,统计结果放在匿名对象中。两种写法。
Console.WriteLine("---------第⼀种写法");
var TermAvgScore_1 = (from l in lst
group l by new { Term = l.Term, Course = l.Course } into grouped
orderby grouped.Average(m => m.Score) ascending
orderby grouped.Key.Term descending
select new { Term = grouped.Key.Term, Course = 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 { Term = m.Term, Course = m.Course })
.Select(k => new { Term = k.Key.Term, Course = k.Key.Course, Scores = k.Average(m => m.Score) })
.OrderBy(l => l.Scores).OrderByDescending(l => l.Term);
foreach (var l in TermAvgScore_2)
{
Console.WriteLine("学期:{0},课程{1},均分{2}", l.Term, l.Course, l.Scores);
}
Linq中没有SQL中的Having语句,因此是采⽤where语句对Group后的结果过滤。
原⽂ : www.it165/pro/html/201301/4521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论