我有一个关于如何将以下内容转换为linq的问题。我一直试图了解ASP.NET MVC,对我来说最大的障碍之一就是Linq。
CREATE PROCEDURE [dbo].[SelectAvailableCoops]
@startDate DATETIME, @endDate DATETIME
AS
BEGIN
SET NOCOUNT ON;
SELECT ID, coopName, coopCPN
FROM tbl_Coops
WHERE ID NOT IN (SELECT coopID
FROM tbl_bookings
WHERE (startDate <= @startDate AND endDate >= @startDate)
OR (startDate < @endDate AND endDate >= @endDate)
OR (@startDate <= startDate AND @endDate >= endDate)
)
END
GO
如果有帮助,目前我的行动结果中有以下内容。
BookingsListVM bookinglist = new BookingsListVM();
//bookinglist.Customers = db.Customers.ToList();
bookinglist.Customers = (from c in db.Customers select c).ToList();
bookinglist.CustomerBookings = (from cb in db.CustomerBookings select cb).ToList();
bookinglist.Coops = (from co in db.Coops select co).ToList();
答案 0 :(得分:0)
这里有一个
db.Customers.Where(x=> db.CustomerBookings.Any(a=> a.coopID != x.Id &&
((a.StartDate <= startDate && a.EndDate >= enddate)
||
(a.StartDate < endDate && a.EndDate >= enddate)
||
(a.StartDate <= startDate && a.EndDate >= endDate)
)));
答案 1 :(得分:0)
另一个答案看起来更好,但这是一个稍长的解决方案,希望更容易理解。
//Returns IDs
var bookingIds = tbl_bookings
.Where(b => b.ReleaseDate <= startDate && b.ReleaseDate >= endDate
|| (b.ReleaseDate < endDate && b.ReleaseDate >= endDate)
|| (b.ReleaseDate <= startDate && b.ReleaseDate >= endDate))
.Select(b => b.Id);
//Returns anonymous object list containing all records
//with an ID not found in previous result set
var bookings = tbl_bookings.Where(b => !bookingIds.Contains(b.Id))
.Select(b => new { ID = b.Id, CoopName = b.Id, coopCPN = b.Id })
.ToList();