原文: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
实现代码:
- void Main()
- {
- 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)=>
- {
- 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));
- /*
- A B C
- 1 1 a
- 1 2 a
- 1 3 a
- 2 7 b
- 2 8 b
- */
- }
- class temp
- {
- public int A{get;set;}
- public int B{get;set;}
- public string C{get;set;}
- }
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, seed, func 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 seed, func 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);
}