C# i分组<;TKey,远程通讯>;总数
我有一个模型:C# i分组<;TKey,远程通讯>;总数,c#,linq,C#,Linq,我有一个模型: public class MyModel { public string Name { get; set; } public string Type { get; set; } public int SqFeet { get; set; } public decimal Cost { get; set; } public decimal CostPerFoot { get; set; } } 我有那个种型号的清单: private Lis
public class MyModel
{
public string Name { get; set; }
public string Type { get; set; }
public int SqFeet { get; set; }
public decimal Cost { get; set; }
public decimal CostPerFoot { get; set; }
}
我有那个种型号的清单:private List<MyModel> _myList;
public List<MyModel> MyList
{
get { return _myList; }
set
{
_myList = value;
OnPropertyChanged();
}
}
现在我的问题是:
MyList
中是否可以显示不同属性的不同总计;所以我要问的是,我想看看平均成本,总英尺和总成本。我该怎么做?您可以获得创建匿名类型的每个组的总数,平均值为CostPerFoot
、总面积为SqFeet
和总面积为cost
:
var totals = MyList.GroupBy(l => l.Name).Select(g => new
{
Name = g.Key,
AverageCostPerFoot = g.Average(m => m.CostPerFoot),
TotalFeet = g.Sum(m => m.SqFeet),
TotalCost = g.Sum(m => m.Cost)
});
既然你在问题中也明确地按名称分组,那么你想要每个分组的值吗?你能解释一下你在问什么吗?基本上我是问你是否想要Arturo在回答中告诉你的内容
var totals = MyList.GroupBy(l => l.Name).Select(g => new
{
Name = g.Key,
AverageCostPerFoot = g.Average(m => m.CostPerFoot),
TotalFeet = g.Sum(m => m.SqFeet),
TotalCost = g.Sum(m => m.Cost)
});