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,B=3,B=4,C="b"},B=5,new temp{ A=2,B=6,B=7,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","jim" };
var query = Enumerable.Repeat(arr,3000); //重复值的集合
foreach (var item in query)
//Console.WriteLine(string.Join("、",item));
Console.WriteLine(item.Aggregate((current,next)));
stopwatch.Stop(); // 停止监视
TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
Console.WriteLine("运行{0}秒",timespan.TotalSeconds);
}
int[] num = { 1,5,6 };
int BaseC = 20;
//保障不为负数
//var r = num.Aggregate(BaseC,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,next) =>
{
Console.WriteLine("current:{0},next:{1}",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,6}序列的项,结果是:" + r.ToString());
Console.WriteLine("=========================");
原文链接:https://www.f2er.com/javaschema/285387.html