Linq 巧用 Max,Sum

IList<int> intList = new List<int>() { 10, 21, 30, 45, 50, 87 };

            var sum1 = intList.Sum(s =>
            {
                if (s % 2 == 0)
                {
                    return s;
                }
                return 0;
            });
            Console.WriteLine("偶数和为:" + sum1);

            var count = intList.Sum(s =>
            {
                if (s % 2 == 0)
                {
                    return 1;
                }
                return 0;
            });
            Console.WriteLine("偶数的个数为:" + count);

            var i1 = intList.Max(m =>
            {
                if (m % 2 == 0)
                {
                    return m;
                }
                return 0;
            });
            Console.WriteLine("最大的偶数为:" + i1);

 

posted @ 2017-12-30 20:49  热敷哥  阅读(325)  评论(0编辑  收藏  举报