I have 3 MySql tables: Students
, Classes
and StudentsInClasses
.
The Entity Framework translates these into two entities
How about:
var query = from @class in db.Classes
from student in @class.Students
select new { ClassId = @class.ID, Student = student };
var lookup = query.ToLookup(x => x.ClassId,
x => x.Student);
(I suspect a Lookup
is more appropriate than a HashSet
here.)
EDIT: If you don't want to use query expressions:
var query = db.Classes
.SelectMany(@class => @class.Students,
(@class, student) => new { ClassId = @class.ID,
Student = student });