子句 | 说明 | ||
---|---|---|---|
from | 指定数据源和范围变量(类似于迭代变量)。 | ||
where | 基于由逻辑 AND 和 OR 运算符(&& 或 | )分隔的一个或多个布尔表达式筛选源元素。 | |
select | 指定执行查询时,所返回序列中元素的类型和形状。 | ||
group | 根据指定的密钥值对查询结果分组。 | ||
into | 提供可作为对 join、group 或 select 子句结果引用的标识符。 | ||
orderby | 根据元素类型的默认比较器对查询结果进行升序或降序排序。 | ||
join | 基于两个指定匹配条件间的相等比较而联接两个数据源。 | ||
let | 引入范围变量,在查询表达式中存储子表达式结果。 | ||
in | join 子句中的上下文关键字。 | ||
on | join 子句中的上下文关键字。 | ||
equals | join 子句中的上下文关键字。 | ||
by | group 子句中的上下文关键字。 | ||
ascending | orderby 子句中的上下文关键字。 | ||
descending | orderby 子句中的上下文关键字。 |
class WhereSample
{
static void Main()
{
// Simple data source. Arrays support IEnumerable<T>.
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// Simple query with one predicate in where clause.
var queryLowNums =
from num in numbers
where num < 5
select num;
// Execute the query.
foreach (var s in queryLowNums)
{
Console.Write(s.ToString() + " ");
}
}
}
//Output: 4 1 3 2 0
Enumerable.Where 方法 (System.Linq)
List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" };
IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);
foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
/*
This code produces the following output:
apple
mango
grape
*/