我没有明确定义复合表,因为EF会自动处理它,但是如何从一个表中查询结果与另一个表的ID:请举例:
public class Student { public int Id {get; set;} public string Name{get; set;} public ICollectionCourses {get; set;} } public class Course { public int Id {get; set;} public string Name {get; set;} public ICollection Students {get; set;} }
如何获得id = 3的学生的所有课程我提出了这个问题:
var studentCourses = from s in context.Students join c in context.Courses on s.Id equals ?? what?
我应该定义连接表吗?
// Return all courses where at least one student has a specific id. var studentId = 3; var courses = context.Courses.Where(x => x.Students.Any(y => y.Id == studentId));
要么
var studentId = 3; var courses = context.Students.Where(x => x.Id == studentId).SelectMany(x => x.Courses);
我应该定义连接表吗?
不,除非它包含任何感兴趣的内容.它是否添加了比表之间的连接更多的信息,例如DateAdded
?在那种情况下,我会为表创建一个实体.但不是这样.