除了Linq中的Sql查询

时间:2016-08-01 19:33:18

标签: entity-framework linq

我正在使用bellow sql获取可用空间:

select rooms.number  
from rooms  

EXCEPT 

(SELECT rooms.number 
 FROM dbo.Bookings 
 INNER JOIN dbo.Rooms ON Extent1.RoomId = dbo.Rooms.Id 
 WHERE 
     (Bookings.StartDate <= '2014-12-01') 
     AND (Bookings.EndDate >= '2016-12-05') 
)

你能帮我转换成Linq吗? (比如&#34; var list =来自db ......&#34;

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个:

var start = DateTime.ParseExact("20141201", 
                          "yyyyMMdd", 
                           CultureInfo.InvariantCulture); 
var end = DateTime.ParseExact("20141205", 
                          "yyyyMMdd", 
                           CultureInfo.InvariantCulture);

var list = dbContext.Rooms
    .Except(dbContext.Bookings
        .Where(e => e.StartDate >= start)
        .Where(e => e.EndDate <= end)
        .Select(e => e.Room))
    .SELECT(e => e.Number)
    .ToList();