I have this query:
(from a in SickDays
join b in Class.Where(p => p.ID == myId) on a.Class_ID equals b.ID
join c in Student on a.Student_ID equals c.ID
group a by new { c.Name, c.Order } into ac
select new { Count = ac.Count(), Name = ac.Key.Name, Order = ac.Key.Order }
).OrderBy(f => f.Order)
This returns:
Count | Name | Order
3 | Dave | a
2 | John | b
7 | Sally| c
However I’d like it to return this:
Count | Name | Order
3 | Dave | a
2 | John | b
7 | Sally| c
0 | Mark | d
0 | Betty| e
UPDATE:
This is what Students, Classes and SickDays look like using @sixlettervariables example:
var Students = new List<Student>()
{
new Student { Id = 1, Name = "Al", Order = 'a' },
new Student { Id = 2, Name = "Betty", Order = 'b' },
new Student { Id = 3, Name = "Charles", Order = 'c' },
};
var Classes = new List<Class>()
{
new Class { Id = 1, Title = "A100" },
new Class { Id = 2, Title = "A200" },
};
var SickDays = new List<SickDay>()
{
new SickDay { Id = 1, StudentId = 1, ClassId = 1 },
new SickDay { Id = 2, StudentId = 1, ClassId = 1 },
new SickDay { Id = 3, StudentId = 1, ClassId = 2 },
new SickDay { Id = 4, StudentId = 1, ClassId = 2 },
new SickDay { Id = 5, StudentId = 2, ClassId = 1 },
};
Ok the above is now correct, so sorry for the confusion!
This is known as a Left Outer Join. In this case, every Student is required even if they have no Sick Days. So, we would start with the students, perform a left outer join with the sick days, and then return the count of the classes which matched the ID you had specified: