Linq的Aggregate方法使用示例

标签: Linq C#
发布时间: 2013/2/22 9:36:50

Aggregate 根据输入的表达式获取一个聚合值

List<int> ls3 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
 
var b1 = ls3.Aggregate((x, y) => x + y );//55 累加
var b2 = ls3.Aggregate((x, y) => x * y);//3628800 累乘
 
public class a
{
 
  
 
static void Main(string[] args)
{
 
List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 4 } };
 
  
 
var b3 = ls1.Aggregate<T1>((x, y) => new T1 { Value = x.Value + y.Value });
 
System.Console.WriteLine(b3.Value); //7
 
}
}
 
  
 
public class T1
{
 
public string Name;
public int Value;
 
}


赞助商