Linq之Sum用法新体会

  1、简单应用,求数组的和,示例:

    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 
    double numSum = numbers.Sum();
 
    Console.WriteLine("The sum of the numbers is {0}.", numSum);

  那么,求其中除以2等于0元素。按我平时的思路,就会这样写:

   int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0};

   double numSum = numbers.Where(x=>x%2==0).Count();

  其实,可以这样写的;

 public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

  Enumerab类的扩张方法

  Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate),
原文地址:https://www.cnblogs.com/zhangyuanbo12358/p/4356994.html