gpt4 book ai didi

vb.net - 如何搜索字典键的一部分?

转载 作者:行者123 更新时间:2023-12-01 17:19:16 24 4
gpt4 key购买 nike

有人可以告诉我,如何在字典(在 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

如果我只知道键的一部分(例如“Bolognese”)或只知道单词(例如“Bolo”)的一部分,如何在完整键中搜索这部分内容?

最佳答案

您可以使用Any()检查是否有任何条目的键包含“Bolognese”

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

要获取键仅包含“Bolognese”的字典子集:

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

要获取包含“Bolognese”的所有条目的价格列表:

Dim pricesForAllThingsBolognese = PriceList _
.Where(Function(x) x.Key.Contains("Bolognese")) _
.Select(Function(x) x.Value) _
.ToList()

关于vb.net - 如何搜索字典键的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17998321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com