代码
var list= db.SCs
.GroupJoin(
(
db.Courses.GroupJoin(
db.Teachers,
Teacher => Teacher.TNo,
Course => Course.TNo,
(c, t) => new { Course = c, Teacher = t })
.SelectMany(
ct => ct.Teacher.DefaultIfEmpty(),
(c, t) => new { Course = c.Course, Teacher = t})
),
SCs => SCs.CNo,
ct => ct.Course.CNo,
(sc, ct) => new { sc = sc, ct = ct })
.SelectMany(
scct => scct.ct.DefaultIfEmpty(),
(sc, ct) => new { sc = sc.sc, ct = ct })
.GroupJoin(
db.Students,
scct => scct.sc.SNo,
Student => Student.SNo,
(scct, s) => new { scct = scct, Student = s })
.SelectMany(
sccts => sccts.Student.DefaultIfEmpty(),
(scct, s) => new { scct=scct.scct, Student = s })
.Select(s => new
{
sNo = s.scct.sc.SNo,
cNo = s.scct.sc.CNo,
score = s.scct.sc.Score,
cName = s.scct.ct.Course.CName,
tNo = s.scct.ct.Course.TNo,
tName = s.scct.ct.Teacher.TName,
sName = s.Student.SName
})
//先按照指定的列进行排序【尽量使用唯一的键例如:主键】
.OrderBy(sc => "sc.cno")
//跳过指定条数【数量*(页码-1)】
.Skip(pageSize * (pageNumber - 1))
//返回指定条数
.Take(pageSize)
.ToList();
说明
通过使用需要被左联的表(也就是 。left join xx on x.yy=xx.yy
中的xx
),不断GroupJoin
主表
错了
是使用主表不断GroupJoin
子表