Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# i分组<;TKey,远程通讯>;总数_C#_Linq - Fatal编程技术网

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)
});