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
实现代码:
- 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);}
- LINQ Aggregate 取集合中连续递增记录
- LINQ Aggregate 取集合中连续递增记录
- LINQ中Aggregate的用法
- LINQ中Aggregate的使用实例
- LINQ之Aggregate
- LINQ中的Aggregate语法
- Linq :Aggregate;SequenceEqual;join
- [Linq] Enumerable.Aggregate[转载]
- LINQ Aggregate聚合学习
- 细说Linq之Aggregate
- 矩阵中最长连续递增子序列
- Linq查询datatable的记录集合
- 使用aggregate在MongoDB中查找重复的数据记录
- linq中datetime取日期格式部分
- 求字符串中最长连续递增子数字串
- 一组数中最长连续递增子序列个数
- 求数组中最长连续递增子序列
- linq 分组 ,每组取时间最新的记录
- windows内核情景分析---进程线程
- 企业网站建设的六大价值
- eclipse运行android项目出现"The connection to adb is down, and a severe error has occured."的问题
- hadoop-2.3.0 配置
- 深入理解Java:SimpleDateFormat安全的时间格式化
- LINQ Aggregate 取集合中连续递增记录
- linux shell awk sed 截取需要的内容
- oracle用户密码有效期问题
- windows内核情景分析 --APC
- 集合框架
- android 超强效率、性能优化(转载)
- 广州传智播客0330首期 PHP就业班开班典礼
- C#将可编译为本地机器码
- 孙鑫VC++深入详解(7):对话框2