C# 在单行C中添加列表对象的值#

C# 在单行C中添加列表对象的值#,c#,linq,C#,Linq,我有一个列表对象。我需要在一行代码中添加对象中的值,而不使用foreach或for循环。是否可以使用linq查询 例如:我有一个长度为2的列表对象userCount。我需要通过在列表中添加ManagerCount值来查找TotalManagerCount public class UserCount { public int ManagerCount {get; set;} public int EngineerCount {get; set;} } List<UserCount>

我有一个列表对象。我需要在一行代码中添加对象中的值,而不使用foreach或for循环。是否可以使用linq查询

例如:我有一个长度为2的列表对象userCount。我需要通过在列表中添加ManagerCount值来查找TotalManagerCount

public class UserCount
{
public int ManagerCount {get; set;}
public int EngineerCount {get; set;}
}

List<UserCount> userCount = new List<UserCount>();

int TotalManagerCount = ??
int TotalEngineerCount = ??
公共类用户计数
{
public int ManagerCount{get;set;}
public int EngineerCount{get;set;}
}
List userCount=new List();
int TotalManagerCount=??
int TotalEngineerCount=??
提前谢谢

Dinesh.

使用LINQ功能:

int TotalManagerCount = userCount.Sum(x=>x.ManagerCount);
int TotalEngineerCount = userCount.Sum(x=>x.EngineerCount);
使用Linq的功能

int TotalManagerCount = userCount.Sum(item => item.ManagerCount);
int TotalEngineerCount = userCount.Sum(item => item.EngineerCount);
您是否尝试userCount.Sum(s=>s.ManagerCount)?