【发布时间】:2015-05-23 08:34:45
【问题描述】:
我有一个MultiValueDictionary<string, string>,我正在尝试按值获取键。
var dic = na.prevNext; // Getter to get MultiValueDictionary
string nodePointingToThisOne = "";
foreach (var item in dic)
{
if(item.Value == "test")
{
nodePointingToThisOne = item.Key;
}
break;
}
这不起作用,所以我尝试了 Linq:
string nodePointingToThisOne = dic.Where(x => x.Value == this.nodeID).Select(x => x.Key);
但在这两个上我都收到此错误:Operator '==' cannot be applied to operands of type 'System.Collections.Generic.IReadOnlyCollection<string>' and 'string'
所以我的问题是如何使这种比较适用于只读集合?我知道如果一个密钥存在多次,我会遇到问题,但我现在将问题减少到这个问题。
我读过 Get Dictionary key by using the dictionary value
LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
Getting key of value of a generic Dictionary?
Get key from value - Dictionary<string, List<string>>
但他们处理的是“普通”字典。
【问题讨论】:
标签: c# .net linq dictionary