简体   繁体   中英

Why does using Linq Group By Sum give me a cast error?

I try using 2 queries to get the result of a subtraction, then a group by sum. The logic seems good but I have the following error: unable to implicitly convert type 'System.Collection.Generic.List' <> 'to' int '

My requests:

var postesList = (from od in db.tbl_607_order_details
                          join o in db.tbl_607_order on od.FK_ID_order equals o.ID
                          where o.order_number == contract
                          select new OrderWizardViewModel
                          {
                              idPoste = od.ID,
                              posteNumber = od.poste_number,
                              contractQ = od.order_quantity,
                              recievedQ = od.recieved_quantity,
                              lifeTime = od.gaz_lifetime,
                              stockMini = od.stock_mini,
                              shippingDelay = od.shipping_delays,
                              onTheRoadQ = 0
                          }).OrderBy(t => t.posteNumber).ToList();

        foreach (var item in postesList)
        {
            item.onTheRoadQ = (from od in db.tbl_607_order_details
                               join srd in db.tbl_607_shipping_request_details on od.ID equals srd.FK_ID_order_details
                               where item.idPoste == srd.FK_ID_order_details & srd.shipping_quantity > srd.reception_quantity
                               select new
                               {
                                   quantity = (srd.shipping_quantity - srd.reception_quantity.Value)
                               }).GroupBy(t => t.quantity).Select(x => new { Sum = x.Sum(y => y.quantity) }).ToList();
        }

I want fill postesList.onTheRoadQ with the sum of shipping_quantity - reception_quantity

Some idea?

All your statement returns a List, Just Sum in the end to summ all the values of the list.

item.onTheRoadQ = (from od in db.tbl_607_order_details
                  join srd in db.tbl_607_shipping_request_details on od.ID equals srd.FK_ID_order_details
                  where item.idPoste == srd.FK_ID_order_details & srd.shipping_quantity > srd.reception_quantity
                  select srd.shipping_quantity - srd.reception_quantity.Value
                  ).Sum()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM