group子句
var students = new[]
{
new {LName="Jones",FName="Mary",Age=23, Major="History"},
new {LName="Smith",FName="Bob",Age=20, Major="Comsci"},
new {LName="Fleming",FName="Carol",Age=21, Major="History"}
};
//var query = from student in students
// orderby student.Age
// select student.LName;
var query = from student in students
group student by student.Major;
foreach(var a in query)
{
Console.WriteLine("{0}",a.Key);//a.Key分组键
foreach(var t in a)
Console.WriteLine(" {0},{1}",t.LName,t.FName);
}
Console.ReadKey();
let子句 let子句接受一个表达式的运算并且把它赋值给一个需要在其他运算中使用的标识符。
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 6, 7, 8, 9 };
var someInts = from a in groupA
from b in groupB
let num = a + b
where num >= 11
where a != 4
select new { a, b, num };
foreach (var a in someInts)
{
Console.WriteLine(a);
}
Console.ReadKey();