在vb.net中将查询结果转换为字典



什么是正确的语法。ToDictionary以Dictionary(Of String,String)的形式返回以下内容,其中ShortDesc是关键字,CurrentFrontSymbol是值

 Dim dicQuery As Dictionary(Of String, String) = (From d In toolkitEntities.currentfrontcommoditysymbols
                                                     Select d.ShortDesc, d.CurrentFrontSymbol)

更新

下面的函数query和下面的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

由于select中没有特殊投影,因此可以在集合上调用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())

相关内容

  • 没有找到相关文章

最新更新