gpt4 book ai didi

c# - 如何检查 Dictionary>() 对象中的字符串值?

转载 作者:太空狗 更新时间:2023-10-29 23:14:08 26 4
gpt4 key购买 nike

为简单起见,我有以下字典,我为每个字符串键填充了未知数量的字符串。我还有以下字符串列表。

var dict = new Dictionary<string, List<string[]>>();
IList<string> headerList = new List<string>();

如何检查列表中的字符串是否是字典中的值?我的数据看起来与此类似:

 Key           Value
----- ------------------
Car honda, volks, benz
Truck chevy, ford

例如,我需要检查“honda”是否包含在字典值中。我在想我需要做类似下面的事情来查看值是否包含一个列表,其中包含有问题的字符串。请记住,我是 C# 的新手。

    foreach (string header in headerList)
{
// This is wrong, I don't know what to put in the if statement
if (dict.ContainsValue(r => r.Contains(header)))
{
// do stuff
}
}

最佳答案

John Odom 是正确的,您需要一个列表。

我建议您使用 HashSet<string>在内部用于 Diictionary 的值.例如。 Dictionary<string, HashSet<string>>

然后当涉及到查询时,你可以这样做:

 headerList.Where(x => dict.Values.Any(d => d.Contains(x))).ToList()

看看.NET Nested Loops vs Hash Lookups性能比较

关于c# - 如何检查 Dictionary<string, List<string[]>>() 对象中的字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856950/

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