加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:20:46 所属栏目:百科 来源:网络整理
导读:.ToDictionary的正确语法是将以下内容作为Dictionary(Of String,String)返回,其中ShortDesc是键,CurrentFrontSymbol是值 Dim dicQuery As Dictionary(Of String,String) = (From d In toolkitEntities.currentfrontcommoditysymbols Select d.ShortDesc,d.Cu
.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())

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读