First item in a SortedDictionary?

前端 未结 2 718
梦谈多话
梦谈多话 2021-01-21 04:10

There are many, many threads on ways to get the \"first\" item from a Dictionary, and various answers as to why such a thing is not really a good idea beca

2条回答
  •  庸人自扰
    2021-01-21 04:26

    The problem is that a SortedDictionary is indeed ordered by the key. But that does not mean that you can access it via index. So if you can't use LINQ:

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

    Otherwise you could simply use First/ FirstOrDefault.

    Sidenote: since a KeyValuePair(Of Tkey, TValue) is a structure, therefore a value type, it is never null/Nothing. You could check for an empty dictionary in this ugly way:

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

    So it's much more readable to use If FRs.Count = 0 Then ....


    Update: if you just want the key or value at a given index you can use:

    Dim firstSchedule As Date = FRs.Keys(0)
    

    or the first Date in it:

    Dim firstDate As ScheduleItem = FRs.Values(0)
    

    On this way you could actually get both via index even without LINQ:

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

    Disclaimer: according to my question here this works only if you have imported System.Linq, then Enumerable.ElementAt is used implicitly which enumerates a sequence to find an item via index if the type doesn't implement IList(Of T). So don't use it in this case.

提交回复
热议问题