0

好的,我在 LinqPad 中测试过的这个 LINQ 语句有一个错误,它工作正常。错误是:

无法创建“HostelApp.Models.bookingLines”类型的常量值。此上下文仅支持原始类型或枚举类型。

代码在这里:

var availableFBunks = from b in this.bunks
      where b.gender == "F" && 
           !this.bookingLines.Any(
                l => (l.bunkID == b.bunkID) && (l.date == DateTime.Today.AddDays(i)))
      select b;

SQL 语句:

-- Region Parameters
DECLARE @p0 NVarChar(1000) = 'F'
DECLARE @p1 DateTime = '2013-11-07 12:00:00 AM'
-- EndRegion

SELECT b.bunkDesc AS Bunk_Name
FROM bunks AS b
WHERE (gender = @p0) AND NOT EXISTS
    (SELECT *
     FROM bookingLines AS l
     WHERE l.bunkID = b.bunkID and (l.date = @p1))
4

1 回答 1

0

let您可以使用关键字在 LINQ 中执行 IN/NOT IN 查询

var availableBunks = from bunk in bunks
        let bl = from bookingLine in bookingLines
            where bookingLine.Date == DateTime.Today.AddDays(d)
            select bookingLine.BunkId
        where bunk.Gender == "F" && !bl.Contains(bunk.BunkId)
        select bunk;

let 子句(C# 参考)

于 2013-11-06T04:44:00.650 回答