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

原文: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 plain copy
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值