迭代LINQ to Entities结果集时函数未知



当我执行这个查询,然后尝试迭代它的结果时,我会得到下面的错误:

Dim recs = MonitorContext.DataRecords.OrderBy(Function(dr) dr.INDEX).ThenBy(Function(dr) dr.TIMESTAMP). _
Where(Function(dr) dr.TYPE = If(type, dr.TYPE)). _
Where(Function(i) i.INDEX > 2 And i.INDEX < 8). _
Where(Function(dr) dr.INDEX >= If(startIndex, dr.INDEX) And dr.INDEX <= If(endIndex, dr.INDEX)). _
Where(Function(dr) dr.TIMESTAMP >= If(startTime, dr.TIMESTAMP))

错误发生在这里:

For Each rec As DataRecord In recs.ToList()

错误为:

EntityCommandExecutionException:

{"为函数指定的参数值无效[参数#=3,函数名称(如果已知)=case]"}

我的数据记录如下:

Public Class DataRecord
    Public Property Id As Integer
    Public Property INDEX As Integer
    Public Property DeviceId As Integer
    Public Property TYPE As String
    Public Property TIMESTAMP As DateTime
    Public Property DI1 As Double
    Public Property DI2 As Double
    Public Property DI3 As Double
    Public Property DI4 As Double
    ...
    ...
    Public Property GSM As Double
    Public Property P As Double
    Public Property E As Double
    Public Property V As Double
    Public Property I As Double
End Class

当然是删节的。大约有20名Double成员只是遗留下来的,没有发挥任何作用。我有足够的勇气通过EF 6在VB.NET中对SqlCe数据库执行此操作。

事实证明,VB合并If()函数在某种程度上被误译了。当我更换时

Where(Function(dr) If(startTime, dr.TIMESTAMP) >= startTime)

带有

Where(Function(dr) startTime Is Nothing OrElse dr.TIMESTAMP >= startTime)

以及所有其他使用If()Where子句,一切都很顺利。

最新更新