在使用“from”子句或“join”子句连接多对多表时,是否存在差异?返回的结果集是相同的。
例如,在一个多对多关系中包括三个表(包括一个联接表):学生、学生课程和课程:
var query = from student in context.Students
from studentcourse in student.StudentCourses
where studentcourse.CourseID == 4
select student;
var query = from student in context.Students
join studentcourse in context.StudentCourses
on student.StudentID equals studentcourse.StudentID
where studentcourse.CourseID == 4
select student;
- 哪一个是最佳实践?
- 性能方面,哪一个更好?我还没有使用SQL Profiler来测试它们。
我避免使用Include有两个原因:
- 我通常需要有条件地包含,因此使用上述两种技术。
- 根据我的了解,Include返回整个相关表,可能会影响性能。