I am working on a C# project using LINQToSQL,But right now I am having a problem with the following query:
var groupByQuery = (from c in db.Customers
join o in db.Orders on c.CustomerID equals o.CustomerID into lf1
from or in lf1.DefaultIfEmpty()
group c by c.CustomerID into g
select new
{
CustomerID = g.Key,
Count = g.Count()
}
).OrderBy(x => x.Count);
As you can see I am doing a LEFT OUTER JOIN
and grouping by the CustomerID
, and everything goes well so far. But when I see the results I realize that the Customers
that does not have any Order
, has a "1" in their Count
field.
Why is that? The Count
field is supposed to have a "0" in these cases, What am I doing wrong?
I found this questions here:
linq-count-query-returns-a-1-instead-of-a-0
linq-to-sql-joining-query-returning-1-instead-of-0
but none of them has been helpful to me, I hope someone can help, thank you in advance.