简体   繁体   English

将SQL转换为LINQ不起作用

[英]convert SQL to LINQ not working

I want to convert this SQL code to LINQ. 我想将此SQL代码转换为LINQ。 Here is my SQL code: 这是我的SQL代码:

SELECT Rooms.RoomName AS RoomNo, Beds.BedName AS Beds, Rooms.RoomType, ISNULL(CheckIn.CheckIntatus,'') AS Status 
FROM CheckIn
INNER JOIN GuestBeds ON CheckIn.GuestBedId = GuestBeds.Id
AND (CheckIn.CheckInStatus = 1 OR CheckIn.CheckIntatus = 2 OR CheckIn.CheckSIntatus = 3) 
RIGHT JOIN Beds ON GuestBeds.BedId = Beds.Id
INNER JOIN Rooms ON Beds.RoomId = Rooms.Id
LEFT JOIN Guests ON CheckIn.GuestId = Guests.Id
WHERE Beds.Active = 1 AND Rooms.Active = 1
ORDER BY RoomName, Beds

It works well which means it shows all the RoomName with CheckInStatus. 它运行良好,这意味着它将显示带有CheckInStatus的所有RoomName。 If the Room is not presence in CheckIn table, ot will return the status as Null. 如果“签到”表中不存在“房间”,则ot将返回状态为Null。 So I want to convert the code to LINQ. 所以我想将代码转换为LINQ。 SO here is my LINQ code: 所以这是我的LINQ代码:

from b in Beds 
join w in Rooms on b.RoomsId equals w.Id
where (a.CheckInStatus == 3 || a.CheckInStatus == 1 || a.CheckInStatus == 2)
join p in GuestBeds on b.Id equals p.BedId
join a in CheckIn on p.Id equals a.GuestBedId 
join t in Guests on a.GuestId equals t.Id
where b.Active == true && w.Active == true
orderby w.RoomName
select new
{
    RoomName = w.RoomName,
    BedName = b.BedName,
    Status = a.CheckInStatus
}

It didnt worked like the first code. 它没有像第一个代码那样工作。 It only show the data which contain CheckInStatus. 它仅显示包含CheckInStatus的数据。 I want it to show all the RoomName inside Room database 我希望它显示Room数据库中的所有RoomName

Normally I would post some rules for converting SQL to LINQ but this is complicated enough I think I'd need to make new rules. 通常,我会发布一些将SQL转换为LINQ的规则,但这非常复杂,我认为我需要制定新规则。 I commented out the references to Guests because as a LEFT JOIN it has no bearing on the answer. 我注释了对Guests的引用,因为作为LEFT JOIN它与答案无关。

Pull out the WHERE on individual tables and make them sub-queries: 在单个表上拉出WHERE并使其成为子查询:

var ActiveBeds = Beds.Where(b => b.Active == 1);
var ActiveRooms = Rooms.Where(r => r.Active == 1);

In LINQ, a RIGHT JOIN must be done by flipping the join to be a left join, so we will create the two sides as sub-queries. 在LINQ中,必须通过将RIGHT JOIN翻转为左RIGHT JOIN来完成RIGHT JOIN ,因此我们将把这两个边创建为子查询。

Left side of RIGHT JOIN : RIGHT JOIN左侧:

Translate the JOIN conditions that aren't part of an equi-join into a LINQ where clause on the appropriate tables (alternately this could be a subquery as above). 将不属于等JOIN条件转换为适当表上的LINQ where子句(或者,这可以是上面的子查询)。 The LEFT JOIN becomes a LINQ join / from ... DefaultIfEmpty() phrase, but as noted above isn't needed. LEFT JOIN就变成了LINQ join / from ... DefaultIfEmpty()语句,但正如上面提到的没有必要的。

var CheckInsGuestBedsGuests = from c in CheckIn
                              where (c.CheckInStatus == 1 || c.CheckInStatus == 2 || c.CheckInStatus == 3)
                              join gb in GuestBeds on c.GuestBedId equals gb.Id
                              //join g in Guests on c.GuestId equals g.Id into gj
                              //from g in gj.DefaultIfEmpty()
                              select new { c, gb /*, g */ };

Right side of RIGHT JOIN : RIGHT JOINRIGHT JOIN

The other side of the RIGHT JOIN includes an INNER JOIN so put them together in a sub-query: RIGHT JOIN的另一端包括一个INNER JOIN因此将它们放到一个子查询中:

var ActiveBedsRooms = from b in ActiveBeds
                      join r in ActiveRooms on b.RoomId equals r.Id
                      select new { b, r };

Finally, flip the sub-queries to create a left join using the same idiom as above: 最后,使用与上面相同的惯用法翻转子查询以创建左联接:

var ans = from br in ActiveBedsRooms
          join cgbg in CheckInsGuestBedsGuests on br.b.Id equals cgbg.gb.BedId into cgbgj
          from cgbg in cgbgj.DefaultIfEmpty()
          select new {
              RoomNo = br.r.RoomName,
              Beds = br.b.BedName,
              br.r.RoomType,
              Status = cgbg.c.CheckInStatus
          };

NOTE: If you were not using LINQ to SQL, the Status expression would fail when cgbg is null and you would need 注意:如果您不使用LINQ to SQL,则当cgbgnull时, Status表达式将失败,并且您需要

              Status = cgbg?.c.CheckInStatus

but unfortunately LINQ to SQL/EF doesn't handle the null conditional operators yet. 但不幸的是,LINQ to SQL / EF尚未处理空条件运算符。

BTW, nice query - brings back memories of when I used to write hotel front desk software :) 顺便说一句,很好的查询-带回了我以前写酒店前台软件的回忆:)

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

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