如何搜索字典键的一部分? [英] How to search for a part of a dictionary key?
本文介绍了如何搜索字典键的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我,我怎么只能在字典中(在VB.NET中)搜索键的一部分?
Could someone please tell me, how I can search for only a part of a key in a dictionary (in VB.NET)?
我使用以下示例代码:
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
如果我只知道键的一部分(例如博洛涅塞)或仅单词的一部分(例如 Bolo),如何在完整键中搜索这一部分?
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?
推荐答案
您可以使用 Any()
If Not PriceList.Where(Function(x) x.Key.Contains("Bolognese")).Any()
Console.WriteLine("No Bolognese, sorry")
End If
要获取包含仅Bolognese:
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()
这篇关于如何搜索字典键的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文