Linq加入,组和总和

时间:2012-06-04 08:41:00

标签: c# linq linq-to-objects

我有这个: 我有三个包含数据订单和订单地址的列表。

我想加入这些名单。

Contents = from o in Orders
           join a in addresses on o.DeliveryAddressId equals a.AddressId
           //join ol in ordersLines on o.OrderId equals ol.OrderId 
           let ol = ShopOrderLines.Where(x=>x.OrderId == o.OrderId)
           select new Order
           {
            OrderId = o.OrderId,
            CreatedDate = o.CreatedDate,
            Status = o.Status,
            CustomerComment = o.CustomerComment,
            AdminComment = o.AdminComment,
            PaymentType = (PaymentType) o.PaymentStatus,
            PaymentStatus = (PaymentStatus) o.PaymentType,
            Firstname = a.Firstname,
            Lastname = a.Lastname,
            Sum = ol.Any() ? ol.Sum(x=>x.Quantity * x.Price) : 0
            }; 

这很好但我也想为每个订单添加orderLines的总和。

像这样的东西。 sum =(ol.Vat * ol.Quantity)+(ol.Quantity + ol.Price)

我想我必须将结果分组才能计算??

任何?

1 个答案:

答案 0 :(得分:0)

我更改了我的代码并添加了ol = ShopOrderLines.Where(x => x.OrderId == o.OrderId)这给了我一个列表om订单行。