相关疑难解决方法(0)

仅使用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; }
}
Run Code Online (Sandbox Code Playgroud)

现在我想TotalQuantityOrder类上引入一个readonly属性,它应该总结所有OrderDetails的数量.

我想知道是否有比这更好的"LINQ方式":

public …
Run Code Online (Sandbox Code Playgroud)

.net c# linq

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

linq ×1