【问题标题】:Changing my stored procedure to Linq将我的存储过程更改为 Linq
【发布时间】:2018-02-09 18:32:46
【问题描述】:

我有一个关于如何将以下内容转换为 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();

【问题讨论】:

    标签: c# asp.net-mvc linq


    【解决方案1】:

    这里有一个

    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)
    )));
    

    【讨论】:

    • 非常感谢
    【解决方案2】:

    另一个答案看起来更好,但这里有一个稍长的解决方案,希望更容易理解。

      //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();
    

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2013-10-22
    • 1970-01-01
    相关资源
    最近更新 更多