LINQ语句无法创建类型的常量值

时间:2013-11-06 02:53:45

标签: c# asp.net linq asp.net-mvc-4

好的,我在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))

1 个答案:

答案 0 :(得分: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 clause (C# Reference)