[英]Sum List<T> Properties of each item in List<T>
Please refer to ff. 请参考ff。 codes: 代码:
MainObj: MainObj:
public class MainObj {
public string Name { get; set; }
public int SomeProperty { get; set; }
public List<SubObj> Subojects { get; set; }
}
SubObj: 子对象:
public class SubObj {
public string Description { get; set; }
public decimal Value { get; set; }
}
QUESTION: I have a List<MainObj>
. 问题:我有一个List<MainObj>
。 Each item has equal number of SubObj
. 每个项目具有相等数量的SubObj
。 How do I sum the values of each SubObj
from MainObj A
which corresponds to the same index as the SubObj
in the other MainObj
in the list. 如何求和来自MainObj A
的每个SubObj
的值,该值对应于与列表中另一个MainObj
中的SubObj
相同的索引。
To illustrate further: 为了进一步说明:
Results: 结果:
{
Name: "something....",
Value: SUM(MainObj_A.SubObj[0], MainObj_B.SubObj[0] .... MainObj_n.SubObj[0]
},
{
Name: "don't really care...."
Value: SUM(MainObj_A.SubObj[1], MainObj_B.SubObj[1] .... MainObj_n.SubObj[1]
},
{
Name: "don't really care...."
Value: SUM(MainObj_A.SubObj[2], MainObj_B.SubObj[2] .... MainObj_n.SubObj[2]
}
.... so on and so forth
I know I can loop thru each item in MainObj
then perform the sum but I was hoping that there is a simpler way using Linq
. 我知道我可以遍历MainObj
每个项目然后执行求和,但是我希望使用Linq
有更简单的方法。
Sorry if I can't put the right words for my question. 对不起,如果我不能为我的问题说正确的话。 I hope the illustrations helped. 希望插图对您有所帮助。
Every help would be much appreciated. 每一个帮助将不胜感激。 CHEERS! 干杯!
As I understand the question you look for something like this: 据我了解的问题,您正在寻找这样的东西:
var results = list.Select((m, i) => new
{
m.Name,
Value = list.Sum(t => t.SubObj[i].Value)
}).ToList();
This creates a list of an anonymous type containing the name of each main object and the sum of all subobject values with the same index as the main object. 这将创建一个匿名类型的列表,其中包含每个主要对象的名称以及所有与该主要对象具有相同索引的子对象值的总和。
It's not clear from your question, but if you want to sum all subojects (not only as many as there are main objects), you can do it like this: 从您的问题尚不清楚,但是如果您想对所有子对象求和(不仅与主要对象一样多),可以这样做:
var results = Enumerable.Range(0, list[0].SubObj.Count)
.Select(i => list.Sum(m => m.SubObj[i].Value)).ToList();
This gives you a list of the sums (without names, as your examples suggest that you "don't really care" about any names). 这为您提供了总和的列表(没有名称,因为您的示例表明您“不太在乎”任何名称)。
I would extend solution of Rene, to sum over all subitems: 我将扩展Rene的解决方案,以概括所有子项:
var result = list.First().Subojects.Select((m, i) => new
{
Name = i.ToString(),
Value = list.Sum(t => t.Subojects[i].Value)
}).ToList();
i have used it with this values: 我用它与这个值:
var list = new List<MainObj>
{
new MainObj { Name = "A", Subojects = new List<SubObj>{new SubObj{ Value = 1}, new SubObj{ Value = 2}, new SubObj{ Value = 3}}},
new MainObj { Name = "B", Subojects = new List<SubObj>{new SubObj{ Value = 1}, new SubObj{ Value = 2}, new SubObj{ Value = 3}}}
};
And result is: 结果是:
0:2 0:2
1:4 1:4
2:6 2:6
For flexibility and slightly boosted performance, I'd prefer a mixed LINQ-sql way as shown in my answer below. 为了提高灵活性并稍微提高性能,我更喜欢使用混合LINQ-sql方式,如下面的答案所示。
var result = from x in list
where x...
group x by x.SomeProperty
into item
select new {
Id = item.Key,
Name = item.Name,
Value = item.SubObj.Sum(i => i.Value)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.