仅使用 LINQ 对包含数字属性的对象集合求和

发布于 2024-10-31 04:52:41 字数 1521 浏览 0 评论 0原文

我有一个像这样的对象模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-11-07 04:52:41

尝试:

OrderDetails.Select(o => o.Quantity).Aggregate((x, y) => x + y)

如果您不希望每次添加都有新的 Quantity 对象的开销(RE 注释),您可以使用类似的内容:

new Quantity {
    Weight = OrderDetails.Select(o => o.Quantity.Weight).Sum(),
    Volume = OrderDetails.Select(o => o.Quantity.Volume).Sum()
};

不像第一个那么好,并且比仅仅更尴尬(并且更慢?)简单的 foreach 循环。

您可以在 MSDN 上找到有关 Aggregate() 方法的更多信息。

Try:

OrderDetails.Select(o => o.Quantity).Aggregate((x, y) => x + y)

If you'd prefer not to have the overhead of the new Quantity object for each addition (RE comment), you could use something like:

new Quantity {
    Weight = OrderDetails.Select(o => o.Quantity.Weight).Sum(),
    Volume = OrderDetails.Select(o => o.Quantity.Volume).Sum()
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文