[英]group by clause in LINQ
我已经通过以下代码,但不明白组子句如何进行分组
请帮忙。 我是c#的新手。
public static List<Student> GetStudents()
{
// Use a collection initializer to create the data source. Note that each element
// in the list contains an inner sequence of scores.
List<Student> students = new List<Student>
{
new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 72, 81, 60}},
new Student {First="Claire", Last="O'Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {99, 89, 91, 95}},
new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {72, 81, 65, 84}},
new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {97, 89, 85, 82}}
};
return students;
}
List<Student> students = GetStudents();
// Write the query.
var studentQuery =
from student in students
let avg = (int)student.Scores.Average()
group student by (avg == 0 ? 0 : avg / 10);
我不明白StudentQuery是如何生成的。 提前致谢 。
分组是指将数据分组的操作,以便每组中的元素共享一个共同的属性GroupBy
将学生分成小组 - 平均分为0-9,10-19,20-29,30-39等。你应该看看http://msdn.microsoft.com/en-us//library/bb546139.aspx
ps
group student by (avg == 0 ? 0 : avg / 10);
对我来说似乎太过分了。 您可以将其更改为更简单
group student by (avg / 10);
pps:我更喜欢LINQ的其他风格,但它完全是个人选择。 另一种风格是
var studentQuery = students.GroupBy(x => x.Scores.Average() / 10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.