var categories =
from student in students
group student by student.Year into studentGroup
select new { GradeLevel = studentGroup.Key, TotalScore = studentGroup.Sum(s => s.ExamScores.Sum()) };
// Execute the query.
foreach (var cat in categories)
{
Console.WriteLine("Key = {0} Sum = {1}", cat.GradeLevel, cat.TotalScore);
}
from student in students
group student by student.Year into studentGroup
select new { GradeLevel = studentGroup.Key, TotalScore = studentGroup.Sum(s => s.ExamScores.Sum()) };
// Execute the query.
foreach (var cat in categories)
{
Console.WriteLine("Key = {0} Sum = {1}", cat.GradeLevel, cat.TotalScore);
}
string[] words = { "cherry", "apple", "blueberry" };
int shortestWord = words.Min((string w) => w.Length);
int shortestWord = words.Min((string w) => w.Length);
说明:使用Lambda可以在对象列表中查寻符合条件的对象集合或者需要的有关对象列表的信息
本文通过两个示例展示了如何使用Lambda表达式进行数据处理:一是对学生按年级分组并计算总分;二是从字符串数组中找出最短单词长度。Lambda表达式简化了对象列表查询操作。
758

被折叠的 条评论
为什么被折叠?



