Many to many relation linq query in EF

前端 未结 1 396
故里飘歌
故里飘歌 2021-01-29 11:11

I have 3 tables Student, Course, and the linking table StudentCourse, how do i return all the courses given a student Id = 1 but also include row where the student id might not

1条回答
  •  一整个雨季
    2021-01-29 11:34

    Given that this is EF and your latest clarification you should have a Courses navigation property on your Student entity and a Students navigation on your Course entity that would allow you to do the following:

    var students =  db.Students
                      .Single(x=> x.Id == 1)
                      .Courses.SelectMany(c=> c.Students)
                      .Distinct();
    

    0 讨论(0)
提交回复
热议问题