02.
Console.WriteLine(
"---------第一种写法"
);
03.
var
TermAvgScore_1 = (from l
in
lst
04.
group
l by
new
{
Term = l.Term, Course = l.Course } into grouped
05.
orderby
grouped.Average(m => m.Score) ascending
06.
orderby
grouped.Key.Term descending
07.
select
new
{
Term = grouped.Key.Term, Course = grouped.Key.Course, Scores = grouped.Average(m => m.Score) }).ToList();
08.
foreach
(var
l
in
TermAvgScore_1)
09.
{
10.
Console.WriteLine(
"学期:{0},课程{1},均分{2}"
,
l.Term, l.Course, l.Scores);
11.
}
12.
Console.WriteLine(
"---------第二种写法"
);
13.
var
TermAvgScore_2 = lst.GroupBy(m =>
new
{
Term = m.Term, Course = m.Course })
14.
.Select(k
=>
new
{
Term = k.Key.Term, Course = k.Key.Course, Scores = k.Average(m => m.Score) })
15.
.OrderBy(l
=> l.Scores).OrderByDescending(l => l.Term);
16.
foreach
(var
l
in
TermAvgScore_2)
17.
{
18.
Console.WriteLine(
"学期:{0},课程{1},均分{2}"
,
l.Term, l.Course, l.Scores);
19.
}
Linq中没有SQL中的Having语句,因此是采用where语句对Group后的结果过滤。
原文
: http://www.it165.net/pro/html/201301/4521.html