SortedDictionary 中的第一项?

First item in a SortedDictionary?

关于如何从 Dictionary 中获取 "first" 项的方法有很多, 很多 个线程,以及关于为什么这样的事情的各种答案这不是一个好主意,因为没有内部排序。但是我的是 SortedDictionary,所以这些论点不适用。然而,我找不到比 Dictionary.

更容易从 SortedDictionary 获取第 N 项的方法

这是我的 SD:

FRs As SortedDictionary(Of DateTime, ScheduleItem)

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

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

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

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

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 中的示例 运行。

问题是 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))

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

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

''' <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