I am working on a C# LINQ code using which I am trying to join two tables by grouping by a column in Table1
Below is the SQL query which returns correct results
SELECT
SUM(scr.Count),spl.Name
FROM Table1 scr
,Table2 spl
WHERE scr.StatusId = spl.StatusId
GROUP by spl.Name
Equivalent LINQ which returns incorrect result
(from l in _dataContext.Table1
join r in
_dataContext.Table2 on
new { l.StatusId } equals new { r.StatusId }
into gj
from r in gj.DefaultIfEmpty()
select new
{
l.Name,
Count = gj.Sum(j => j.Count)
}).GroupBy(l => l.Name).Select(x => new DataPoint
{
Name = x.Key,
Y = x.Sum(y => y.Count)
}).ToList();
Wasn't able to figure the error, Is there a better way to get the results equivalent to SQL query above?
DefaultIfEmptydoes.JOINin your SQL code as well.