Can I have a where clause that works on a grandson in a LINQ query?

I have three tables, each of which is associated with a primary key and a foreign key, such as TestId and UserTestId, etc.

Exam > Test > UserTest

That is my understanding. I can use LINQ to get data from them as follows:

        var exams = _examsRepository
       .GetAll()
       .Where(q => q.SubjectId == subjectId)
       .Include(q => q.Tests.Select(t => t.UserTests))
       .ToList();

This will select all exams, exam tests, and user tests for those tests in which SubjectId == subjectID

Is there any possible way that I could limit this so that it only shows data when user tests had a UserId of 123?

If the answer is no, should I rewrite this LINQ to go to _userTestsRepository first and then work in the other direction up and not down?

+1
1

, , , , , . include , , .
:

var data = (from ex in context.exams
           join t in context.tests on ex.Id equals test.ExamID
           join ut in context.userTests on t.Id equals ut.TestId
           where ut.UserId = 123
           select new {ex, ut}).ToList();
+1

Source: https://habr.com/ru/post/1533316/


All Articles