LINQ Aggregate 取集合中连续递增记录

来源:互联网 发布:淘宝客如何推广商品 编辑:程序博客网 时间:2024/05/17 02:37

原文:http://blog.csdn.net/q107770540/article/details/6625243

需求:


例如
A B C
----------
1 1 a
1 2 a
1 3 a   
1 4 b
1 5 a
2 6 a
2 7 b
2 8 b

得出结果: 要A.C相同 ,B连续递增

A B C
----------
1 1 a
1 2 a
1 3 a   
2 7 b
2 8 b  

实现代码:


[csharp] view plaincopy
  1. void Main()  
  2. {  
  3.   
  4.   var list=new List<temp>  
  5.     {  
  6.      new temp{ A=1, B=1, C="a"},  
  7.      new temp{ A=1, B=2, C="a"},  
  8.      new temp{ A=1, B=3, C="a"},  
  9.      new temp{ A=1, B=4, C="b"},  
  10.      new temp{ A=1, B=5, C="a"},  
  11.      new temp{ A=2, B=6, C="a"},  
  12.      new temp{ A=2, B=7, C="b"},  
  13.      new temp{ A=2, B=8, C="b"}  
  14.     };  
  15.      var result= new List<temp>();  
  16.     var query=list.Aggregate((m,n)=>  
  17.     {  
  18.       if(m.A ==n.A && m.C==n.C)  
  19.       {  
  20.         if(m.B==n.B-1)  
  21.         {  
  22.         result.Add(m);  
  23.         result.Add(n);  
  24.          return n;  
  25.          }  
  26.          else  
  27.          {  
  28.            return m;  
  29.          }  
  30.       }  
  31.      else{return n;}  
  32.     }  
  33.     );  
  34.     Console.WriteLine("A\tB\tC");  
  35.     result.Distinct().ToList().ForEach(r=>Console.WriteLine("{0}\t{1}\t{2}",r.A,r.B,r.C));  
  36.      /* 
  37.      A    B    C 
  38.     1    1    a 
  39.     1    2    a 
  40.     1    3    a 
  41.     2    7    b 
  42.     2    8    b  
  43.      */  
  44. }  
  45. class temp  
  46. {  
  47.   public int A{get;set;}  
  48.   public int B{get;set;}  
  49.   public string C{get;set;}  
  50. }  

http://msdn.microsoft.com/zh-cn/library/bb548651(v=vs.110).aspx


另附:http://www.csharp-examples.net/linq-aggregate/

Aggregate (LINQ)

Enumerable.Aggregate is C# version of fold or reduce function. It is extension method fromSystem.Linq namespace.

Aggregate method applies a function to each item of a collection. For example, let's havecollection { 6, 2, 8, 3 } and the function Add (operator +) it does (((6+2)+8)+3) and returns 19.

Aggregate alternative for Sum

Implementation of Sum using Aggregate method. This example use Aggregate method overload with only one parameter func. Into the func parameter there is passed lambda expression (anonymous method) which adds two numbers.

This example is for demonstration purpose only. To compute sum of numbers use ratherEnumerable.Sum.

In this example there is passed named method Add insted of lambda expression.


Aggregate alternative for Average

Implementation of Average using Aggregate method. There is used Aggregate method overload with three parameters, seedfunc and resultSelector.

This example is for demonstration purpose only. To compute average value use ratherEnumerable.Average.


Aggregate Implementation

This is .NET Framework implementation of Enumerable.Aggregate method with only one paramater func.

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");    using (IEnumerator<TSource> e = source.GetEnumerator())    {        if (!e.MoveNext()) throw Error.NoElements();        TSource result = e.Current;        while (e.MoveNext()) {            result = func(result, e.Current);        }        return result;    }}

This is .NET Framework implementation of Enumerable.Aggregate method with three parameters seedfunc and resultSelector.

public static TResult Aggregate<TSource, TAccumulate, TResult>(    this IEnumerable<TSource> source,    TAccumulate seed,    Func<TAccumulate, TSource, TAccumulate> func,    Func<TAccumulate, TResult> resultSelector){    if (source == null) throw Error.ArgumentNull("source");    if (func == null) throw Error.ArgumentNull("func");    if (resultSelector == null) throw Error.ArgumentNull("resultSelector");        TAccumulate result = seed;    foreach (TSource element in source) {        result = func(result, element);    }    return resultSelector(result);}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 充电宝ic坏了怎么办 淘宝店宝贝权重下降怎么办 淘宝卖家评分低怎么办 淘宝买东西客服不理人怎么办 支付宝本次交易嫌疑违规怎么办 支付宝一年的交易总额怎么办 交易关闭钱扣了怎么办 交易猫账号绑定支付宝打不开怎么办 拼多多涉假处罚怎么办 淘宝店铺重复铺货扣6分怎么办 帆布鞋子买大了怎么办 开淘宝店没销量怎么办 开淘宝店没有销量怎么办 淘宝买东西支付密码错了怎么办 淘宝登入密码忘记了怎么办 逛街时手机没电了怎么办 逛街手机没有电了怎么办 卖家拒收退回来怎么办 群英会奖池钱不够买超了怎么办 三星s8卡顿严重怎么办 三星s8变卡了怎么办 新开店铺没有人怎么办 电视显示没有usb设备怎么办 华为平板电脑触摸屏没反应怎么办 平板电脑液晶显示屏坏了怎么办 6p的home键失灵怎么办 读书郎屏幕坏了怎么办 苹果平板激活锁忘了怎么办 平板电脑充电插口坏了怎么办? 电视频幕花了怎么办 苹果笔记本电脑硬盘坏了怎么办 mac屏幕压坏了怎么办 新电脑连不上网怎么办 新买电脑后悔了怎么办 装显卡后玩游戏黑屏怎么办 顺丰寄主机坏了怎么办 征信账户忘记了怎么办 疑似qq和疑似应用宝怎么办 荣大3608速印机卸版故障怎么办 配的近视眼镜看不清电脑屏幕怎么办 cad命令反应很慢怎么办