搜索字典值并返回满足条件的键列表

McN*_*ade 7 c# linq arrays dictionary list

我在搜索字典时遇到了问题.

Dictionary<string, string[]> dic = new Dictionary<string, string[]>(){
    {"a", new string[](){"a_val","2"}},
    {"b", new string[](){"b_val","1"}},
    {"b", new string[](){"b_val","0"}}
};
Run Code Online (Sandbox Code Playgroud)

我需要返回一个键列表,其中stingArray [1]元素的值大于"1",如:

List<string> list = new List<string>{
    "a"
};
Run Code Online (Sandbox Code Playgroud)

因为我并不擅长使用LINQ,所以我目前的解决方案是遍历Dictionary并将键添加到新列表中.但是这种方法看起来很丑陋,我正试图找到问题的另一种解决方案.

Mig*_*oom 13

没有任何错误处理:

var list = dic.Where(x => int.Parse(x.Value[1]) > 1)
              .Select(x => x.Key)
              .ToList();
Run Code Online (Sandbox Code Playgroud)

使用该Where语句,Value[1]将过滤大于1 的条目,并使用该Select语句选择此条目中的密钥.至少该集合将List使用该ToList方法转换为a .

有关详细信息,请查看C#中的101 LINQ示例