LINQ 查询操作符详解
1. where 子句
where 子句用于排除不满足指定条件的项。它的语法如下:
where BooleanExpression关于 where 子句,有以下重要信息:
- 一个查询表达式可以包含任意数量的 where 子句,只要它们位于from...let...where部分。
- 一个项必须满足所有的 where 子句,才能避免被排除。
以下代码展示了一个包含两个 where 子句的查询表达式示例:
static void Main() { var groupA = new[] { 3, 4, 5, 6 }; var groupB = new[] { 6, 7, 8, 9 }; var someInts = from int a in groupA from int b in groupB let sum = a + b where sum >= 11 // Condition 1 where a == 4 // Condition 2 select new {a, b, sum}; foreach (var a in someInts)