我在以下查询中获取有关mscorlib.dll错误的InvalidOperation,并且在我的生活中无法找出原因。
这是Class I用List填充List(Of);
Public Class ProjectionPerformance
Public SymbolId As Long
Public Name As String
Public ProjectionDate As String
Public ActualRange As Double
Public ProjectedRange As Double
End Class
除了我转换为字符串的日期
之外,这些类型与表中的类型相匹配以下是LINQ查询的函数
Public Shared Function GetRangeProjectionPerformance(Optional daysToRetrieve As Integer = 100) As Dictionary(Of Long, List(Of ProjectionPerformance))
Dim todaysDate As Date = DateTime.Now.Date
Dim lookbackDate As Date = todaysDate.AddDays(daysToRetrieve * -1)
Using ctx As New ProjectionsEntities
Dim query = (From d In ctx.projections
Where d.SymbolId <= 42 AndAlso d.Date >= lookbackDate
Join t In ctx.symbols On d.SymbolId Equals t.Id
Let actualRange = d.ActualHigh - d.ActualLow
Let projectedRange = d.HighProjection - d.LowProjection
Select New With {
d.Date,
d.SymbolId,
t.Name,
projectedRange,
actualRange}).GroupBy(Function(o) o.SymbolId).ToDictionary(Function(p) p.Key,
Function(x) x.Select(Function(y) New ProjectionPerformance() With {
.SymbolId = y.SymbolId,
.ProjectionDate = y.Date.ToString(),
.Name = y.Name,
.ActualRange = y.actualRange,
.ProjectedRange = y.projectedRange
}).ToList())
Return query
End Using
End Function
我在LINQ查询的这一部分上得到了这个错误(我假设那部分在VS2013中以绿色突出显示)
Function(x) x.Select(Function(y) New ProjectionPerformance() With {
我是否必须检索实际字段值并删除Let语句并在Dictionary调用的List函数中执行计算?
答案 0 :(得分:1)
根据您的评论,我怀疑问题在于您尝试将空值分配给ProjectionPerformance
中的双重属性。如果您按如下方式更改代码,它应该可以工作:
Dim query = (From d In ctx.projections
Where d.SymbolId <= 42 AndAlso d.Date >= lookbackDate
Join t In ctx.symbols On d.SymbolId Equals t.Id
Let actualRange = d.ActualHigh - d.ActualLow
Let projectedRange = d.HighProjection - d.LowProjection
Select New With {
d.Date,
d.SymbolId,
t.Name,
projectedRange,
actualRange}).GroupBy(Function(o) o.SymbolId).ToDictionary(Function(p) p.Key,
Function(x) x.Select(Function(y) New ProjectionPerformance() With {
.SymbolId = y.SymbolId,
.ProjectionDate = y.Date.ToString(),
.Name = y.Name,
.ActualRange = If(y.actualRange, 0.0),
.ProjectedRange = y.projectedRange
}).ToList())
为了在运行程序之前发现此类错误,您应该将OPTION STRICT设置为ON(在项目或文件级别)。