IList<int> datasource = new List<int>();        datasource.Add(1);        datasource.Add(2);        datasource.Add(7);        datasource.Add(3);        datasource.Add(3);        datasource.Add(4);        datasource.Add(5);        datasource.Add(5);
        IList<int> datasource2 = new List<int>();        datasource2.Add(1);        datasource2.Add(2);        datasource2.Add(7);        datasource2.Add(3);        datasource2.Add(3);        datasource2.Add(4);        IList<IList<int>> collection = new List<IList<int>> { datasource, datasource2 };
        //----------------------这两句的意思--------------------------------
        bool isExist = collection.Aggregate(false, (prev, x) => prev ? prev : x.Any(y => y == 5));      
        bool isExist1 = collection.Aggregate(true, (prev, x) => prev ? x.Any(y => y == 5) : prev);帮忙解释一下以下两句的意思,越详细越好 bool isExist = collection.Aggregate(false, (prev, x) => prev ? prev : x.Any(y => y == 5));      
bool isExist1 = collection.Aggregate(true, (prev, x) => prev ? x.Any(y => y == 5) : prev);

解决方案 »

  1.   

    三元   Linq   完毕
      

  2.   

    对序列应用累加器函数,提取第一个元素作为初始化累计结果,然后循环遍历其他元素,并依次调用 func 委托进行累计操作。func 有两个参数,第一个参数为上一次 func 的执行结果,第二个参数为要处理的集合元素
    bool isExist = collection.Aggregate(false, (prev, x) => prev ? prev : x.Any(y => y == 5));
    这里的prev都是为正整数,永远都为true,所以prev ? prev : x.Any(y => y == 5));永远都是prev,为true;bool isExist1 = collection.Aggregate(true, (prev, x) => prev ? x.Any(y => y == 5) : prev);prev 还是一样永远都为true,所以执行x.Any(y => y == 5),x的结果集是datasource ,datasource 2
    第一次遍历datasource 的时候有等于5所以为true,第二次遍历datasource2的结果集中没有等于5所以为false,最后返回false
      

  3.   

    The Aggregate<TSource, TAccumulate, TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>) method makes it simple to perform a calculation over a sequence of values. This method works by calling func one time for each element in source. Each time func is called, Aggregate<TSource, TAccumulate, TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>) passes both the element from the sequence and an aggregated value (as the first argument to func). The value of the seed parameter is used as the initial aggregate value. The result of func replaces the previous aggregated value. The final result of func is passed to resultSelector to obtain the final result of Aggregate<TSource, TAccumulate, TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>).