使用linq进行数组查询
输出数组中全部的偶数并升序输出结果
写法1:
int[] numbers = { 5, 10, 8, 3, 6, 12 }; //查询的数组
var numqurey = from num in numbers
where num % 2 == 0 //按照条件过滤
orderby num
select num;
foreach (var num in numqurey)
{
Console.WriteLine(num + " "); //输出查询结果
}
输出结果:
写法2:
int[] numbers = { 5, 10, 8, 3, 6, 12 };
var numqurey = numbers.Where(n => n % 2 == 0).OrderBy(n => n);
foreach (var num in numqurey)
{
Console.WriteLine(num + " ");
}
输出结果: