6

Could someone please tell me, how I can search for only a part of a key in a dictionary (in VB.NET)?

I use the following sample code:

    Dim PriceList As New Dictionary(Of String, Double)(System.StringComparer.OrdinalIgnoreCase)

    PriceList.Add("Spaghetti alla carbonara", 21.65)
    PriceList.Add("Spaghetti aglio e olio", 22.65)
    PriceList.Add("Spaghetti alla napoletana", 23.65)
    PriceList.Add("Spaghetti alla puttanesca ", 24.65)
    PriceList.Add("Spaghetti alla gricia ", 25.65)
    PriceList.Add("Spaghetti alle vongole", 26.65)
    PriceList.Add("Spaghetti Bolognese", 27.65)

    If PriceList.ContainsKey("spaghetti bolognese") Then
        Dim price As Double = PriceList.Item("spaghetti bolognese")
        Console.WriteLine("Found, price: " & price)
    End If

    If Not PriceList.ContainsKey("Bolognese") Then
        Console.WriteLine("How can I search for only a part of a key?")
    End If

If I only know a part of the key like "Bolognese" or just a part of word like "Bolo", how can I search for this part in the complete key?

PeterCo
  • 910
  • 2
  • 20
  • 36

1 Answers1

12

You can check if there's any entry which a key containing "Bolognese" using Any()

If Not PriceList.Where(Function(x) x.Key.Contains("Bolognese")).Any()
    Console.WriteLine("No Bolognese, sorry")
End If

To get a subset of the dictionary with keys containing "Bolognese" only:

Dim subsetOfDictionary = PriceList _ 
        .Where(Function(x) x.Key.Contains("Bolognese")) _ 
        .ToDictionary(Function(x) x.Key, Function(x) x.Value)

To get the list of prices for all entries containing "Bolognese":

Dim pricesForAllThingsBolognese = PriceList _
        .Where(Function(x) x.Key.Contains("Bolognese")) _
        .Select(Function(x) x.Value) _
        .ToList()
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Thank you very much! And how can I get the value (the price in this case) for "Bolognese"? – PeterCo Aug 02 '13 at 08:13
  • Thanks again! The third example works perfect! The second example gives something like _Overload resolution failed because no accessible 'ToDictionary' accepts this number of type arguments_ . If I change it to Dim subsetOfDictionary As Dictionary(Of String, KeyValuePair(Of String, Double)) = PriceList _ .Where(Function(x) x.Key.Contains("Bolognese")) _ .ToDictionary(Function(x) x.Key) it works fine too. – PeterCo Aug 02 '13 at 13:10
  • I'm new on SO. Should I edit your second example or is it up to you? – PeterCo Aug 02 '13 at 13:12
  • @PeterCo Thanks, I edited it myself, which is the preferred way. Editing other member's content is usually only done to correct typos and improve readability or clarity. – Dennis Traub Aug 02 '13 at 13:24
  • Great, now it works - please remove the not valid comma in "Function(x), x.Value" in the second example. I can't edit this typo, because SO tells me an edit must have at least 6 characters .-) – PeterCo Aug 02 '13 at 13:47
  • @PeterCo Done, thanks. Mind accepting the answer if it was helpful? – Dennis Traub Aug 02 '13 at 14:06