如何将查询更改为实体框架

本文关键字:实体 框架 查询 | 更新日期: 2023-09-27 18:29:11

我想将此SQL查询更改为实体框架查询:

SELECT 
    dbo.ClassTiming.StartTime, 
    dbo.ClassTiming.EndTime, 
    dbo.Employee.StaffName, 
    dbo.Department.DepartmentName, 
    dbo.Class.ClassName, 
    dbo.Section.SectionName, 
    dbo.WeekDay.DayName 
FROM dbo.Timetable 
INNER JOIN dbo.ClassTiming 
    ON dbo.Timetable.ClassTimingId = dbo.ClassTiming.Id 
INNER JOIN dbo.Employee 
    ON dbo.Timetable.StaffId = dbo.Employee.StaffID 
INNER JOIN dbo.Department 
    ON dbo.Timetable.DepartmentId = dbo.Department.id 
INNER JOIN dbo.Section 
    ON dbo.Timetable.SectionID = dbo.Section.ID 
INNER JOIN dbo.Class 
    ON dbo.Timetable.ClassID = dbo.Class.ID 
INNER JOIN dbo.WeekDay 
    ON dbo.Timetable.WeekDayId = dbo.WeekDay.Id

如何将查询更改为实体框架

这应该能在中工作

var db = new YourDbContext();
var result = (from t in db.Timetables
                join c in db.ClassTimings on t.ClassTimingId equals c.Id
                join e in db.Employees on t.StaffId equals e.StaffID
                join d in db.Departments on t.DepartmentId equals d.Id
                join s in db.Sections on t.SectionID equals s.Id
                join cl in db.Classes on t.ClassID equals cl.Id
                join w in db.WeekDays on t.WeekDayId equals w.Id
                select
                    new { c.StartTime,
                          c.EndTime, e.StaffName,
                          d.DepartmentName,
                          cl.ClassName,
                          s.SectionName,
                          w.DayName
                        }
            ).ToList();