from
var lowNums = from num in numbers
where num < 5
select num;
numbers 是数据源,而 num 是范围变量。 请注意,这两个变量都是强类型
List<Student> students = new List<Student>
{
new Student {LastName="Omelchenko", Scores= new List<int> {97, 72, 81, 60}},
new Student {LastName="O‘Donnell", Scores= new List<int> {75, 84, 91, 39}},
new Student {LastName="Mortensen", Scores= new List<int> {88, 94, 65, 85}},
new Student {LastName="Garcia", Scores= new List<int> {97, 89, 85, 82}},
new Student {LastName="Beebe", Scores= new List<int> {35, 72, 91, 70}}
};
// Use a compound from to access the inner sequence within each element.
// Note the similarity to a nested foreach statement.
var scoreQuery = from student in students
from score in student.Scores
where score > 90
select new { Last = student.LastName, score };
where
条件
where 子句用在查询表达式中,用于指定将在查询表达式中返回数据源中的哪些元素。
它将一个布尔条件(“谓词”)应用于每个源元素(由范围变量引用),并返回满足指定条件的元素。 一个查询表达式可以包含多个 where 子句,一个子句可以包含多个谓词子表达式。