仅使用 LINQ 对包含数字属性的对象集合求和
我有一个像这样的对象模型:
public class Quantity
{
public decimal Weight { get; set; }
public decimal Volume { get; set; }
// perhaps more decimals...
public static Quantity operator +(Quantity quantity1, Quantity quantity2)
{
return new Quantity()
{
Weight = quantity1.Weight + quantity2.Weight,
Volume = quantity1.Volume + quantity2.Volume
};
}
}
public class OrderDetail
{
public Quantity Quantity { get; set; }
}
public class Order
{
public IEnumerable<OrderDetail> OrderDetails { get; set; }
}
现在我想在 Order
类上引入一个只读属性 TotalQuantity
,该属性应该总结所有 OrderDetails 的数量。
我想知道是否有比这更好的“LINQ 方式”:
public class Order
{
// ...
public Quantity TotalQuantity
{
get
{
Quantity totalQuantity = new Quantity();
if (OrderDetails != null)
{
totalQuantity.Weight =
OrderDetails.Sum(o => o.Quantity.Weight);
totalQuantity.Volume =
OrderDetails.Sum(o => o.Quantity.Volume);
}
return totalQuantity;
}
}
}
这不是一个很好的解决方案,因为它会迭代 OrderDetails 两次。并且不支持类似的操作(即使 Quantity 类中提供了 + 运算符):
Quantity totalQuantity = OrderDetails.Sum(o => o.Quantity); // doesn't compile
是否有更好的方法在 LINQ 中构建总和?
(仅出于理论兴趣,一个简单的 foreach 循环当然也能很好地完成其工作。)
感谢您的反馈!
I have an object model like this:
public class Quantity
{
public decimal Weight { get; set; }
public decimal Volume { get; set; }
// perhaps more decimals...
public static Quantity operator +(Quantity quantity1, Quantity quantity2)
{
return new Quantity()
{
Weight = quantity1.Weight + quantity2.Weight,
Volume = quantity1.Volume + quantity2.Volume
};
}
}
public class OrderDetail
{
public Quantity Quantity { get; set; }
}
public class Order
{
public IEnumerable<OrderDetail> OrderDetails { get; set; }
}
Now I want to introduce a readonly property TotalQuantity
on the Order
class which should sum up the quantities of all OrderDetails.
I am wondering if there is better "LINQ way" than this:
public class Order
{
// ...
public Quantity TotalQuantity
{
get
{
Quantity totalQuantity = new Quantity();
if (OrderDetails != null)
{
totalQuantity.Weight =
OrderDetails.Sum(o => o.Quantity.Weight);
totalQuantity.Volume =
OrderDetails.Sum(o => o.Quantity.Volume);
}
return totalQuantity;
}
}
}
It's not a nice solution as it iterates twice through the OrderDetails. And something like this is not supported (even though a + operator is provided in the Quantity class):
Quantity totalQuantity = OrderDetails.Sum(o => o.Quantity); // doesn't compile
Is there a better way to build the total sum in LINQ?
(Just for theoretical interest, a simple foreach loop would also do its job well of course.)
Thanks for feedback!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
如果您不希望每次添加都有新的 Quantity 对象的开销(RE 注释),您可以使用类似的内容:
不像第一个那么好,并且比仅仅更尴尬(并且更慢?)简单的
foreach
循环。您可以在 MSDN 上找到有关
Aggregate()
方法的更多信息。Try:
If you'd prefer not to have the overhead of the new Quantity object for each addition (RE comment), you could use something like:
Not as nice as the first one, and slightly more awkward (and slower?) than just the plain
foreach
loop.You can find more about the
Aggregate()
method on MSDN.