最早使用到Lambda表达式是因为一个需求:
如果一个数组是:int[] s = new int[]{1,3,5,9,14,16,22};
例如只想要这个数组中小于15的元素然后重新组装成一个数组或者直接让s返回一个新数组该怎么截取?

最开始的想法就是将这个s遍历一遍然后判断下再来重新组装成新的数组.好麻烦是不是? 于是便百度到了一个叫做Lambda的东西, 所以用了之后效果如下:

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {
 5         int[] s = new int []{ 1,3,5,9,14,16,22 };
 6         var result = from n in s where n < 15 select n;
 7         int[] b = result.ToArray();
 8         for (int i = 0; i < b.Length; i++) 
 9         {
10             Console.WriteLine(b[i]);
11         }
12         Console.ReadKey();
13     }
14 }
View Code

相关文章:

  • 2021-08-19
  • 2022-12-23
  • 2021-08-12
  • 2021-05-29
  • 2021-12-27
  • 2021-11-16
  • 2021-11-29
猜你喜欢
  • 2021-09-28
  • 2021-09-11
  • 2022-12-23
  • 2021-06-09
  • 2021-10-06
  • 2021-12-07
相关资源
相似解决方案