.ToDictionary的正确语法是将以下内容作为Dictionary(Of String,String)返回,其中ShortDesc是键,CurrentFrontSymbol是值 Dim dicQuery As Dictionary(Of String, String) = (From d In toolkitEntities.currentfrontcommoditysymbols Se
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, d.SymbolId, t.Name, 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())