select sum(pos.balance * coalesce(units.formula,1)) as on_order,poi.uniq_key FROM mvw_PoItems poi join mvw_PoMain pom on pom.ponum = poi.ponum join mvw_SupplierInfo si on pom.UNIQSUPNO = si.UNIQSUPNO join mvw_PoItemsSchedule pos on poi.uniqlnno = Pos.uniqlnno join mvw_Inventory inv on inv.uniq_key = poi.uniq_key left join mvw_Units units on [units].[from] = inv.pur_lunit where pom.postatus not in ('CANCEL','CLOSED') and pos.balance <> 0 and poi.poittype <> 'MRO' and poi.lcancel <> 1 group by poi.uniq_key order by poi.uniq_key
var NotInStatuses = new string[] {"CANCEL","CLOSED"}; NotInStatuses.Dump(); var query = from poi in mvw_PoItems join pom in mvw_PoMain on poi.ponum equals pom.ponum join si in mvw_SupplierInfo on pom.uniqsupno equals si.uniqsupno join pos in mvw_PoItemsSchedule on poi.uniqlnno equals pos.uniqlnno join inv in mvw_Inventory on poi.uniq_key equals inv.uniq_key join units in mvw_Units on inv.pur_lunit equals units.to into unitsjoin from myUnits in unitsjoin.DefaultIfEmpty() where !NotInStatuses.Contains(pom.postatus) && pos.balance != 0 && poi.poittype != "MRO" && poi.lcancel != true orderby poi.uniq_key group new {poi, pos, myUnits} by new {poi.uniq_key} into g select new { uniq_key = g.Key, on_order = g.Sum(x => (decimal?)x.pos.balance * x.myUnits.formula ?? (decimal?)1) }; query.Dump(); //Using LinqPad to try to figure this out.