BPM Expection Issues

Hi All,

Any one able to help me with this one?

I’m having some issues with a LINQ query where if the part number isn’t in the Part Master allow it but I cannot seem to get it to work.

Here is the code

var CalcWeight = (from orderDtl in Db.OrderDtl
          join part in Db.Part on  new { orderDtl.Company, orderDtl.PartNum } equals new { part.Company, part.PartNum } into partleft from part in partleft.DefaultIfEmpty()
                   where orderDtl.OrderNum == OrderNum 
                   select orderDtl.OrderQty * (part.NetWeight != 0 ? part.NetWeight : orderDtl.CustomWeight_c)
                   ).ToList().Sum();
 
 
 using (var txScope = IceContext.CreateDefaultTransactionScope()){
  var OrderHed = Db.OrderHed.With(LockHint.UpdLock).Where(x=>
    x.Company == Session.CompanyID &&
    x.OrderNum == OrderNum).FirstOrDefault();
  
  if (OrderHed != null){
    TotalWeight = Convert.ToDecimal(CalcWeight);
    OrderHed.ParcelWeight_c =  Convert.ToDecimal(CalcWeight); 
  }

}   

All I want it to do is if it doesn’t find a partnumber record then just put zero but I’m not sure where to put the if statement at the top?

Thanks
Aaron.

Well, you have part.netweight != 0 in there. If the part record doesn’t exist, that’s gonna blow up on you.

If it were me, if would return just the field you need in the first call, then do a null chack on those, and it it passes then do any math you need, instead of trying to do it in the call itself. That might make it easier to troubleshoot.

Edit: actually all of it is going to blow up if you don’t make the return nullable. You have to add a ? After the linq statement, then you can check if it’s null without it throwing an exception.

Brandon,

I’m not sure I follow. Please could you show me?

Thanks
Aaron

select orderDtl.OrderQty * (part.NetWeight != 0 ? part.NetWeight : orderDtl.CustomWeight_c)

In this line here, if you are trying to compare, and part doesn’t exists, it’s gonna blow up. See if this works. (it might not, I don’t know if linq with short circuit or not)

select orderDtl.OrderQty * (part.NetWeight != null ?  (part.NetWeight != 0 ? part.NetWeight :orderDtl.CustomWeight_c):orderDtl.CustomWeight_c)

Try with DefaultIfEmpty. Instead of having:

var x = (expression).Sum();

Try:

var x = (expression).DefaultIfEmpty(0).Sum();