LINQ Aggregate聚合学习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var num = Enumerable.Range(1, 5); //start,count
            foreach (var n in num)
                Console.WriteLine(n.ToString() + ",");
            Console.WriteLine("=========================");

            string[] arr = { "jon", "tom", "lucy", "kitty", "jim" };
            var q = arr.Aggregate((current, next) => string.Format("{0}、{1}", current, next));
            Console.WriteLine(q);
            Console.WriteLine("=========================");

            var query = Enumerable.Repeat(arr, 3); //重复值的集合
            foreach (var item in query)
                Console.WriteLine(item.Aggregate((y, x) => string.Format("{0},{1}", y, x)));
            Console.WriteLine("=========================");

            var result = num.Aggregate((total, next) => total * next);
            Console.WriteLine(string.Format("5的阶乘为:{0}", result)); //也就是1*2*3*4*5
            Console.WriteLine("=========================");

            //依次减去序列中的项
            int BaseC = 20;
            var r = num.Aggregate(BaseC, (current, next) => next <= current ? current - next : current);
            Console.WriteLine("20依次减去{1,2,3,4,5}序列的项,结果是:" + r.ToString());
            Console.WriteLine("=========================");

            kk();
            Console.ReadKey();
        }

        static void kk()
        {
            var list = new List<temp>  
            {  
             new temp{ A=1, B=1, C="a"},  new temp{ A=1, B=2, C="a"},  
             new temp{ A=1, B=3, C="a"},  new temp{ A=1, B=4, C="b"},  
             new temp{ A=1, B=5, C="a"},  new temp{ A=2, B=6, C="a"},  
             new temp{ A=2, B=7, C="b"},  new temp{ A=2, B=8, C="b"}  
            };
            var result = new List<temp>();
            var query = list.Aggregate((m, n) =>
            {
                //m=>current(默认第一个),n=>next
                if (m.A == n.A && m.C == n.C)
                {
                    if (m.B == n.B - 1)
                    {
                        result.Add(m);
                        result.Add(n);
                        return n;
                    }
                    else
                    {
                        return m;
                    }
                }
                else
                {
                    return n;
                }
            });
            Console.WriteLine("A\tB\tC");
            result.Distinct().ToList().ForEach(r => Console.WriteLine("{0}\t{1}\t{2}", r.A, r.B, r.C));
        }
    }

    class temp
    {
        public int A { get; set; }
        public int B { get; set; }
        public string C { get; set; }
    }

}
结果如下:

 static void jj()
        {
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start(); //  开始监视代码运行时间
            string[] arr = { "jon", "tom", "lucy", "kitty", "jim" };
            var query = Enumerable.Repeat(arr, 3000); //重复值的集合
            foreach (var item in query)
                //Console.WriteLine(string.Join("、", item));
                Console.WriteLine(item.Aggregate((current, next) => string.Format("{0}、{1}", current, next)));
            stopwatch.Stop(); //  停止监视

            TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间
            Console.WriteLine("运行{0}秒", timespan.TotalSeconds);
        }

int[] num = { 1, 2, 3, 4, 5, 6 };
            int BaseC = 20;
            //保障不为负数
            //var r = num.Aggregate(BaseC, (current, next) => next <= current ? current - next : current);
            //Func<int, int, int> fn = (x, y) => x - y;
            //var r = num.Aggregate(BaseC, fn);
            var r = num.Aggregate(BaseC, (current, next) =>
            {
                Console.WriteLine("current:{0},next:{1}", current, next);
                /*
                 current:20,next:1
                 current:19,next:2
                 current:17,next:3
                 current:14,next:4
                 current:10,next:5
                 current:5,next:6
                 */
                return current - next;
            });

            Console.WriteLine("20依次减去{1,2,3,4,5,6}序列的项,结果是:" + r.ToString());
            Console.WriteLine("=========================");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值