【问题标题】:First item in a SortedDictionary?SortedDictionary 中的第一项?
【发布时间】:2016-04-28 14:54:20
【问题描述】:

关于从Dictionary 获取“第一个”项目的方法有很多,很多 线程,以及关于为什么这样的事情不是一个好主意的各种答案,因为没有内部订购。但我的是SortedDictionary,所以这些论点不适用。然而,我找不到比Dictionary 更容易从SortedDictionary 获取第N 项的方法。

这是我的 SD:

FRs As SortedDictionary(Of DateTime, ScheduleItem)

我看到了一些我应该能够做到的提示:

If FRs.Count = 1 Then
    FirstFR = FRs.Keys(0)

但这在我的代码中是无效的——它说它没有默认值并且不能被索引。 .First 和其他选项似乎都在 LINQ 中,我无法定位。那么有什么简单的方法可以以这种方式访问​​它吗?

注意:所提供的任何解决方案不得使用 LINQ,在许多非 Wintel 平台上并不存在。

【问题讨论】:

    标签: vb.net sorteddictionary


    【解决方案1】:

    问题是SortedDictionary 确实是按键排序的。但这并不意味着您可以通过索引访问它。所以如果你不能使用 LINQ:

    Dim firstFR As KeyValuePair(Of DateTime, ScheduleItem)
    For Each kv In FRs
        firstFR = kv
        Exit For
    Next
    

    否则你可以简单地使用First/FirstOrDefault

    旁注:由于KeyValuePair(Of Tkey, TValue) 是一个结构,因此是一个值类型,它永远不会是null/Nothing。你可以用这种丑陋的方式检查空字典:

    If firstFR.Equals(New KeyValuePair(Of DateTime, ScheduleItem))
        Console.WriteLine("Empty dictionary")
    End If 
    

    因此使用If FRs.Count = 0 Then ... 更具可读性。


    更新:如果您只想要给定索引处的键或值,您可以使用:

    Dim firstSchedule As Date = FRs.Keys(0)
    

    或其中的第一个Date

    Dim firstDate As ScheduleItem = FRs.Values(0)
    

    通过这种方式,即使没有 LINQ,您实际上也可以通过索引获得两者:

    Dim firstFR = new KeyValuePair(Of DateTime, ScheduleItem)(FRs.Keys(0), FRs.Values(0))
    

    免责声明:根据my question here,这仅在您导入System.Linq 时有效,然后隐式使用Enumerable.ElementAt,如果类型未实现IList(Of T),则它会枚举一个序列以通过索引查找项目。所以在这种情况下不要使用它。

    【讨论】:

    • 好的,谢谢蒂姆。又是另一个“他们在想什么”的时刻。实际上,他们真的没有理由不能在默认情况下做到这一点吗?
    • 它在我的代码中不起作用,我收到关于没有默认值或索引的错误。确切地说:'Public Overloads ReadOnly Property Keys As SortedDictionary(Of Date, ScheduleItem).KeyCollection' 没有参数,它的返回类型不能被索引
    • ...当尝试对集合进行反向迭代并删除重复项时,这相当烦人。
    • @MauryMarkowitz:奇怪,你是对的。但它可以编译并为我工作。有趣
    • @MauryMarkowitz:我创建了一个关于这个主题的问题:stackoverflow.com/questions/36933428/…
    【解决方案2】:

    Linq 大多只是一系列扩展方法,所以你可以自己写:

    Imports System
    Imports System.Collections.Generic
    Imports System.Runtime.CompilerServices
    
    Public Module EnumerableExtensions
    
        <Extension()> 
        Public Function FirstValue(Of TKey, TValue)(source As SortedDictionary(Of TKey, TValue)) As TValue
            For Each kvp In source
                Return kvp.Value
            Next
            Return Nothing
        End Function
    
    End Module
    
    Public Module Module1
        Public Sub Main()
            Dim a As SortedDictionary(Of string, string) = new SortedDictionary(Of string, string)
            a.Add("foo", "1 - foo")
            a.Add("bar", "2 - bar")
            Console.WriteLine(a.FirstValue())
        End Sub
    End Module
    

    这是在dotnetfiddle 中运行的示例。

    【讨论】:

      【解决方案3】:

      刚刚实现的例子;还添加了访问最后一个条目的扩展; 这是我的工作代码:

      ''' <summary>
      ''' return the first object of a sortedDictionary
      ''' </summary>
      ''' <typeparam name="TKey">Key tpye</typeparam>
      ''' <typeparam name="TValue">value type</typeparam>
      ''' <param name="source">dictionary</param>
      ''' <returns></returns>
      
      <Extension()>
      Public Function FirstValue(Of TKey, TValue)(source As SortedDictionary(Of TKey, TValue)) As TValue
          For Each kvp As TValue In source.Values
              Return kvp
          Next
          Return Nothing
      End Function
      ''' <summary>
      ''' return the last object of a sortedDictionary
      ''' </summary>
      ''' <typeparam name="TKey">Key type</typeparam>
      ''' <typeparam name="TValue"> value type</typeparam>
      ''' <param name="source"> dictionary</param>
      ''' <returns>the last object or nothing for an empty dictionary</returns>
      <Extension()>
      Public Function lastValue(Of TKey, TValue)(source As SortedDictionary(Of TKey, TValue)) As TValue
          Dim lv As TValue = Nothing
          For Each kvp As TValue In source.Values
              lv = kvp
          Next
          Return lv
      End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多