Count必须是DbConstantExpression或DbParameterReferenceExpression。参数名称:Count

时间:2012-04-23 11:11:05

标签: linq entity-framework linq-to-entities dbcontext

Dim days = (From d In db.ShiftSchedule _
               Where d.Shift1 = 1 And d.WorkDate >= StartDate _
               Order By d.ID _
               Skip SkipValue _
               Select New With {d.ID, d.WorkDate, d.Shift1, _
               .EndDate = (From dd In db.ShiftSchedule _
               Where d.Shift1 = 1 And d.WorkDate >= StartDate _
               Order By d.ID _
               Skip SkipValue - 1 _
               Select dd.WorkDate).First}).First

当我尝试执行上面的LINQ to Entities查询时,我收到以下错误,该代码可能出错。

  

Count必须是DbConstantExpression或DbParameterReferenceExpression。参数名称:Count

2 个答案:

答案 0 :(得分:0)

我认为,你的内部查询应该具有条件 其中 dd .Shift1 = 1和 dd .WorkDate> = StartDate排序依据 dd .ID 代替, 其中d.Shift1 = 1而d.WorkDate> = StartDate _                按d.ID排序

我不确定这是不是错字。

答案 1 :(得分:0)

由于计算出您提供的“跳过”的值,因此将导致异常。如果将值存储在首先计算的变量中,然后再使用该变量,则它应能按预期工作。

所以

Dim actualSkipValue = SkipValue - 1

Dim days = (From d In db.ShiftSchedule _
               Where d.Shift1 = 1 And d.WorkDate >= StartDate _
               Order By d.ID _
               Skip SkipValue _
               Select New With {d.ID, d.WorkDate, d.Shift1, _
               .EndDate = (From dd In db.ShiftSchedule _
               Where d.Shift1 = 1 And d.WorkDate >= StartDate _
               Order By d.ID _
               Skip actualSkipValue _
               Select dd.WorkDate).First}).First

不应引发异常。