linq中的聚合

             //TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
            //求和
            var list = Enumerable.Range(1, 100);
            var count = list.Aggregate((a, b) => a + b);
            Console.WriteLine(count);
            //种子重载
            //TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
            var nums = Enumerable.Range(2, 4);
            var sum = nums.Aggregate(1, (a, b) => a * b);
            Console.WriteLine(sum);
            //实际使用,不合法文件名替换
            var invalidFileName = Path.GetInvalidFileNameChars();
            var replaceResult = invalidFileName.Aggregate( "study<Aggregate>first" , (accmulate, result) => accmulate.Replace(result, '-'));
            Console.WriteLine(replaceResult);
            //反转单词
            string sentence = "the quick brown fox jumps over the lazy dog";
            string[] words = sentence.Split(' ');
            string reversed = words.Aggregate((workingSentence, next) =>
            next + " " + workingSentence);
            Console.WriteLine(reversed);
            //计算偶数个数
            int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
            //统计一个数组中偶数的个数,种子值设置为0,找到偶数就加1
            //0是种子传给total,
            int numEven = ints.Aggregate(1, (total, next) =>
            next % 2 == 0 ? total + 1 : total);
            Console.WriteLine("The number of even integers is: {0}", numEven);
            //reate an array of string of the name charlist 和string.join挺像的
            List<string> listt = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                listt.Add(i.ToString());
            }
            var time1 = Stopwatch.StartNew();
            var concat = listt.Aggregate((a, b) => a + ',' + b);
            time1.Stop();
            Console.WriteLine("Aggregate:" + 1+",用时(毫秒):"+ time1.ElapsedMilliseconds);
            var time2 = Stopwatch.StartNew();
            string concatStr = string.Join(",", listt);
            time2.Stop();
            Console.WriteLine("string.join:"+ 1+ ",用时(毫秒):" + time2.ElapsedMilliseconds);
            /* 放累加源码 比string.join快一点 10条数据
             Aggregate:1,用时(毫秒):39877
             string.join:1,用时(毫秒):4
             public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
             {
               if (source == null)
               {
               throw Error.ArgumentNull("source");
               }
               if (func == null)
               {
               throw Error.ArgumentNull("func");
               }
               TSource result;
               using (IEnumerator<TSource> enumerator = source.GetEnumerator())
               {
               if (!enumerator.MoveNext()) //获取第一个current值。
               {
                    throw Error.NoElements();
               }
               TSource tSource = enumerator.Current; //将第一个值作为全局变量。
               while (enumerator.MoveNext()) //取当前的source的第二个值。
               {
                tSource = func(tSource, enumerator.Current); //然后将第一个值和第二个值调用func委托。
               //取得的值放到全局变量tSource中。
               }
                result = tSource;
               }
                return result;
               }
             */
            string MyStr = "a, b, c, d";
           

            //sum聚合
            long sumt = ints.Sum(x=>(long)x);
            Console.WriteLine(sumt);
            //去重 set键值对,不会添加重复的进去
            // IL_005b: callvirt instance bool class System.Linq.Set`1<!TSource>::Add(!0)
            var nums2 = new int[] { 10, 20, 10, 40, 20, 30 };
            var query = nums2.Distinct();
            foreach (var temp in query)
            {
                Console.WriteLine(temp);
            }

            Console.ReadKey();
Aggregate这个聚合方法很少用到,但有的时候却很实用。
posted @ 2021-11-11 15:23  vba是最好的语言  阅读(63)  评论(0编辑  收藏  举报