C# linq初探 使用linq查询数组中元素
使用linq进行数组查询
输出数组中全部的偶数并升序输出结果
写法1:
int[] numbers = { 5, 10, 8, 3, 6, 12 }; //查询的数组var numqurey = from num in numberswhere num % 2 == 0 //按照条件过滤orderby numselect 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 + " ");}
输出结果: