[转]Linq中GroupBy方法的使用总结

时间:2022-01-19 23:19:32

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