c#查询关键字where 子句的运用
引导语:where是数据库中的一个指令,一般用于用于规定选择的标准。在c#中同样适用,以下是小编整理的c#查询关键字where 子句的运用,欢迎参考阅读!
where 子句用在查询表达式中,用于指定将在查询表达式中返回数据源中的哪些元素。它将一个布尔条件(“谓词”)应用于每个源元素(由范围变量引用),并返回满足指定条件的元素。一个查询表达式可以包含多个 where 子句,一个子句可以包含多个谓词子表达式。
示例
在下面的示例中,where 子句筛选出除小于五的`数字外的所有数字。如果移除 where 子句,则会返回数据源中的所有数字。表达式 num < 5 是应用于每个元素的谓词。
C#
class WhereSample
{
static void Main()
{
// Simple data source. Arrays support IEnumerable.
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() + " ");</