简体   繁体   English

通过内部联接和左侧外部联接将6个表联接在一起-LINQ

[英]Joining 6 tables together with inner joins & a left outer join - LINQ

I have 6 tables. 我有6张桌子。 Like this: 像这样:

在此处输入图片说明

Here is the SQL code that I'm trying to re-code within LINQ. 这是我要在LINQ中重新编码的SQL代码。

SELECT        dbo.TimeTable.Day, dbo.TimeTable.StartTime, dbo.TimeTable.Duration, dbo.Module.ModuleRef, dbo.Module.ModuleName, dbo.Course.CourseRef, dbo.Room.RoomRef, dbo.Room.RoomName, 
                         dbo.Room.RoomFloor, dbo.Room.RoomNumber, dbo.Building.BuildingRef, dbo.Building.BuildingName
FROM            dbo.Room INNER JOIN
                         dbo.TimeTable INNER JOIN
                         dbo.Module ON dbo.TimeTable.ModuleId = dbo.Module.Id ON dbo.Room.Id = dbo.TimeTable.RoomId INNER JOIN
                         dbo.Building ON dbo.Room.BuildingId = dbo.Building.Id LEFT OUTER JOIN
                         dbo.Course INNER JOIN
                         dbo.CourseModule ON dbo.Course.Id = dbo.CourseModule.CourseId ON dbo.Module.Id = dbo.CourseModule.ModuleId

If anyone could point me in the right direction of converting this to a LINQ statement? 是否有人可以指出将其转换为LINQ语句的正确方向? I am new to this concept of linq statements. 我对linq语句的这个概念并不陌生。 Thanks for any help! 谢谢你的帮助!

Try and not get overwhelmed with the fact that you have 6 tables and inner and left joins. 尽量不要因您有6个表以及内部和左连接而感到不知所措。 Try to learn the concepts of joining 2 collections (Inner and Left) and then its just a matter of chaining linq together. 尝试学习将两个集合(内部和左侧)合并的概念,然后将linq链接在一起即可。 Now the code can look a bit complicated but it really isn't. 现在,代码看起来可能有些复杂,但实际上并非如此。

Given the following in memory objects: 给定以下内存对象:

        var rooms = new List<Room>();
        var timeTables = new List<TimeTable>();
        var modules = new List<Module>();
        var buildings = new List<Building>();
        var courses = new List<Course>();
        var courseModules = new List<CourseModule>();

Your linq query might look like the following: 您的linq查询可能如下所示:

           var result = rooms
            .Join(timeTables,
                room => room.Id,
                table => table.RoomId,
                (room, table) => new {room, table})
            .Join(modules,
                arg => arg.table.ModuleId,
                module => module.Id,
                (room_table, module) => new {room_table, module})
            .Join(buildings,
                arg => arg.room_table.room.BuildingId,
                building => building.Id,
                (room_table_module, building) => new {room_table_module, building})
            .GroupJoin(courseModules,
                arg => arg.room_table_module.module.Id,
                coursemodule => coursemodule.ModuleId,
                (room_table_module_building, coursemodules) => new { room_table_module_building, coursemodules})
            .SelectMany(arg => arg.coursemodules.DefaultIfEmpty(),
                (arg, coursemodule) => new { arg.room_table_module_building, coursemodule })
            .Join(courses,
                arg => arg.coursemodule.CourseId,
                course => course.Id,
                (room_table_module_building_coursemodule, course) => new { room_table_module_building_coursemodule, course });

The great part of LinqPad is that you have direct access to the db objects and can play around with your linq queries and see the generated sql. LinqPad的重要之处在于,您可以直接访问db对象,并且可以处理linq查询并查看生成的sql。 You can then take the sql and ensure that the execution plan looks good and can add any indexes that will optimize your queries. 然后,您可以使用sql并确保执行计划看起来不错,并且可以添加将优化查询的任何索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM