has-relation in linq-statement?
I have three tables. Question, Discipline and QuestionHasDisci开发者_Python百科pline. QuestionHasDiscipline holds the relation between Question and Discipline. They all have an unique id-column to identify them.
I am trying to write a linq-statement that returns all the questions that have a certain discipline.
What I have begun doing is this:
var questions = (from q in context.Questions
where (from d in context.QuestionHasDiscipline
where d.QuestionId == q.QuestionId
) ...
But it obviously is horribly wrong. I've tried different approaches but now I turn to the greater minds.. Any suggestions?
You can use .Any()
with a predicate.
from q in context.Questions
where context.QuestionHasDiscipline.Any(d => d.QuestionId == q.QuestionId)
select q;
精彩评论