Nullable Linq

Hi all,

I need some help with a bit of code.

var CalcWeight = (from shipDtl in Db.ShipDtl
                   join part in Db.Part on  shipDtl.PartNum equals part.PartNum
                   join orderDtl in Db.OrderDtl on shipDtl.PartNum equals orderDtl.PartNum
                   where shipDtl.PackNum == PackNum
                   select shipDtl.SellingInventoryShipQty * part.NetWeight
                   ).ToList().Sum();

using (var txScope = IceContext.CreateDefaultTransactionScope())
{

foreach (var ShipDtl in (from ShipDtlRow in Db.ShipDtl
where ShipDtlRow.Company == Session.CompanyID 
&& ShipDtlRow.PackNum == PackNum
select ShipDtlRow))
{
TotalWeight = Convert.ToDecimal(CalcWeight);
ShipDtl.ShippedWeight_c =  Convert.ToDecimal(CalcWeight); 
}


 Db.Validate();
 txScope.Complete();
}   

This is currently working perfectly, but I need to introduce OrderDtl.CustomWeight_c if part.NetWeight is null and display a message if part.NetWeight not null and OrderDtl.CustomWeight_c has been used.

I have done the joins but I’m not sure how to do the other bits…

How would I achieve this? Thank you for your help.

Try something like this

var CalcWeight = (from shipDtl in Db.ShipDtl
                   join part in Db.Part on  new { shipDtl.Company, shipDtl.PartNum } equals new { part.Company, part.PartNum }
                   join orderDtl in Db.OrderDtl on  new { shipDtl.Company, shipDtl.PartNum } equals new { orderDtl.Company, orderDtl.PartNum }
                   where shipDtl.Company == Session.CompanyID && shipDtl.PackNum == PackNum
                   select shipDtl.SellingInventoryShipQty * (part.NetWeight != 0 ? part.NetWeight : orderDtl.CustomWeight_c)
                   ).ToList().Sum();

Also always join on Company. PS: I dont think your OrderDtl is complete, you might need to join to OrderNum, OrderLine, OrderRelNum

Join OrderNum, OrderLine, OrderRelNum onto ShipDtl?

var CalcWeight = (from shipDtl in Db.ShipDtl
                   join part in Db.Part on  new { shipDtl.Company, shipDtl.PartNum } equals new { part.Company, part.PartNum }
                   join orderDtl in Db.OrderDtl on  new { shipDtl.Company, shipDtl.OrderNum, shipDtl.OrderLine, shipDtl.OrderRelNum, shipDtl.PartNum } equals new { orderDtl.Company, orderDtl.OrderNum, orderDtl.OrderLine, orderDtl.OrderRelNum, orderDtl.PartNum }
                   where shipDtl.Company == Session.CompanyID && shipDtl.PackNum == PackNum
                   select shipDtl.SellingInventoryShipQty * (part.NetWeight != 0 ? part.NetWeight : orderDtl.CustomWeight_c)
                   ).ToList().Sum();
1 Like