I have two objects, let’s call them Input and Output
Input has properties Input_ID, Label, and Input_Amt
Output has properties Output_ID and Output_Amt
I want to perform the equivalent SQL statement in LINQ:
SELECT Label, Sum(Added_Amount) as Amount FROM (SELECT I.Label, I.Input_Amt + ISNULL(O.Output_Amt, 0) as Added_Amount FROM Input I LEFT OUTER JOIN Output O ON I.Input_ID = O.Output_ID) GROUP BY Label
For the inner query, I’m writing something like:
var InnerQuery = from i in input join o in output on i.Input_ID equals o.Output_ID into joined from leftjoin in joined.DefaultIfEmpty() select new { Label = i.Label, AddedAmount = (i.Input_Amt + leftjoin.Output_Amt) };
In testing, however, the statement returns null. What gives?
Also, how can I continue the desired query and perform the group after I’ve added my amounts together, all within a single LINQ statement?
Okay, now that I understand what’s going on a bit better, the main problem is that you haven’t got the equivalent of the ISNULL bit. Try this instead: