如何找到字典对象中部分匹配的键?
我试图根据给定的部分或完全匹配的字符串来获取字典中的所有项目。
我尝试了以下代码,但似乎不起作用
a.Where(d => d.Value.Contains(text)).ToDictionary(d => d.Key, d => d.Value);
您能告诉我如何实现这一目标吗?
I am trying to grab all the items in the dictionary based on given string which matches partially or fully.
I tried the following code but doesn't seems to work
a.Where(d => d.Value.Contains(text)).ToDictionary(d => d.Key, d => d.Value);
Can you please tell me how to achieve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您确实想要查找 值 部分匹配的条目,那么您给出的代码应该绝对可以正常工作。如果你看到其他东西,我怀疑你的诊断有缺陷。如果您想查找 key 部分匹配的条目,您只需交换
简短
但完整的程序,演示您给出的代码的工作情况:
输出:
The code you've given should work absolutely fine, assuming you really wanted to find entries where the value had a partial match. If you're seeing something else, I suspect your diagnostics are flawed. If you wanted to find entries where the key had a partial match, you just want to swap
for
Short but complete program demonstrating the code you've given working:
Output: