.ToDictionary的正确语法是将以下内容作为Dictionary(Of String,String)返回,其中ShortDesc是键,CurrentFrontSymbol是值
Dim dicQuery As Dictionary(Of String,String) = (From d In toolkitEntities.currentfrontcommoditysymbols Select d.ShortDesc,d.CurrentFrontSymbol)
更新
并且以下函数可以查询和跟踪For Each循环成为一个LINQ查询吗?
Public Shared Function GetRangeProjectionPerformance(Optional daysToRetrieve As Integer = 100) As Dictionary(Of Integer,List(Of ProjectionPerformance)) Dim todaysDate As Date = DateTime.Now.Date Dim lookbackDate As Date = todaysDate.AddDays(daysToRetrieve * -1) Dim temp As New Dictionary(Of Integer,List(Of ProjectionPerformance)) 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.HighProjection - d.LowProjection Select New With { d.Date,d.SymbolId,t.Name,actualRange}).GroupBy(Function(o) o.SymbolId).ToDictionary(Function(p) p.Key) For Each itm In query Dim rpp As New ProjectionPerformance Dim rppList As New List(Of ProjectionPerformance) If itm.Value.Count > 0 Then For x As Integer = 0 To itm.Value.Count - 1 Dim bb As Integer = Convert.ToInt32(itm.Value(x).SymbolId) With rpp .SymbolId = bb .ProjectionDate = itm.Value(x).Date.ToString() .Name = itm.Value(x).Name .ProjectedRange = itm.Value(x).actualRange End With rppList.Add(rpp) Next End If temp.Add(itm.Key,rppList) Next End Using Return temp End Function
解决方法
由于您在选择中没有特殊投影,因此您只需在集合上调用ToDictionary即可.第一个lambda表达式检索键,第二个表达值.
Dim dicQuery = toolkitEntities.currentfrontcommoditysymbols.ToDictionary( _ Function(x) x.ShortDesc,_ Function(y) y.CurrentFrontSymbol)
至于您的更新:以下查询应该可以获得所需的结果:
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.HighProjection - d.LowProjection Select New With { d.Date,actualRange}).GroupBy(Function(o) o.SymbolId) .ToDictionary(Function(p) p.Key,Function(x) x.Select(Function(y) New ProjectionPerformance() With { .SymbolId = Convert.ToInt32(y.SymbolId),.ProjectionDate = y.Date.ToString(),.Name = y.Name,.ProjectedRange = y.actualRange }).ToList())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。