奇怪的错误迭代空LINQ到EF结果集

时间:2014-09-30 10:52:15

标签: c# linq entity-framework sql-server-ce entity-framework-6

目前我调用System.Data.Entity.Infrastructure.DbQuery<T>上任何空的方法,我得到类型转换,null和DateTime溢出错误。结果集来自SqlCe,我从未在任何完整版本的SQL Server中看到过这一点。如果它是空的,我有什么方法可以避免迭代它,但是为了看它是否为空,我必须至少调用Any(),甚至会触发错误。

这是代码:

    Dim points As New List(Of ChartLinePoint)

    ' ***** Fix starts here *****
    Dim carryOn = monitorContext.DataRecords.Any()
    If Not carryOn Then
        Return points
    End If
    ' ***** Fix ends here *****

    recs = monitorContext.DataRecords. _
    OrderBy(Function(dr) dr.INDEX).ThenBy(Function(dr) dr.TIMESTAMP). _
    Where(Function(i) i.INDEX > 2 And i.INDEX < 8). _
    Where(Function(dr) dr.TIMESTAMP >= startTime). _
    Where(Function(dr) dr.TIMESTAMP < endTime). _
    Where(Function(dr) instrumentIds.Contains(dr.DeviceId))

    For Each rec As DataRecord In recs.ToList()
        Dim point = New ChartLinePoint
        point.Id = rec.Id
        point.Index = rec.INDEX
        point.AdHoc = adHoc
        point.DeviceId = rec.DeviceId
        point.TimeStamp = rec.TIMESTAMP
        point.P = rec.P
        point.E = rec.E
        point.V = rec.V
        point.I = rec.I
        points.Add(point)
    Next
    Return points

似乎允许在添加任何条件之前迭代DbQuery,但不能在之后。

0 个答案:

没有答案