I have two tables, Exam and ExamGroup which have a *:* relationship. I am given a List<Exam> and need to determine if an ExamGroup for this list already exists. So the provided list might be A+B+C, ExamGroups will contain, A+B, A+C, A+B+C+D+E, etc. and I need to determine if A+B+C exists and create it if it doesn't. The code below seems to do it but I'm wondering if it's possible to do it in a single expression?
var givenExams = /* Provided list of exams */
var examGroup = _context
.ExamGroups
.Include(x => x.ExamGroupsExam) // Mapping table for EF Core
.ThenInclude(y => y.Exam)
.AsEnumerable();
examGroup = givenExams.Aggregate(
examGroup,
(current, exam) => current.Where(x => x.Exams.Contains(exam)));
At this point examGroup is either the relevant group (which has an ID and other fields I care about) or null, at which point I know to create it.