【发布时间】:2017-03-10 09:34:22
【问题描述】:
请参考ff。代码:
MainObj:
public class MainObj {
public string Name { get; set; }
public int SomeProperty { get; set; }
public List<SubObj> Subojects { get; set; }
}
子对象:
public class SubObj {
public string Description { get; set; }
public decimal Value { get; set; }
}
问题:我有一个List<MainObj>。每个项目都有相同数量的SubObj。如何将 MainObj A 中的每个 SubObj 的值求和,这与列表中另一个 MainObj 中的 SubObj 对应的索引相同。
进一步说明:
结果:
{
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
我知道我可以遍历MainObj 中的每个项目然后执行求和,但我希望有一个更简单的方法使用Linq。
很抱歉,如果我不能为我的问题提出正确的措辞。我希望插图有所帮助。
每一个帮助将不胜感激。 干杯!
【问题讨论】: