我有两个字典,它们都是
例如,字典1具有键
我尝试使用类似以下代码的linq语句选择它们:
Dictionary<string, List<List<string>>
类型。我想从字典1中选择与字典2的值/条目相匹配的条目范围。例如,字典1具有键
01、01.1
,而字典2在键1
处具有相应的值01、01.1。
首先,我要像这样从字典2的键1获取条目:var dictList = (from entry in DbDict
where entry.Key == id.ToString() // id = 1
select entry).ToList();
我尝试使用类似以下代码的linq语句选择它们:
```
var matchingEntries = (from entry in ExcelDict
where ExcelDict.Keys.Equals(dictList[0].Value)
select entry).ToList();
我尝试了一些类似的方法,但是没有返回任何结果。
如何获取字典1中与字典2的值匹配的键的值对范围?
编辑1:
Dictionary 1:
Key Value
01 "these", "are"
..., ...
01.1 "just", "some"
..., ...
02 "sample", "values"
..., ...
Dictionary 2:
Key Value
1 "01", "01.1"
"foo", "bar"
..., ...
2 "02", "02.21"
"value1" "value2"
编辑2:
期望输出结果:
"01", "01.1"
"foo", "bar"
编辑3:
根据评论中的要求,提供可编译的输入内容。这正是我正在处理的结构:
var dict1 = new Dictionary<string, List<List<string>>>();
dict1.Add("01", new List<List<string>> { new List<string> { "these" }, new List<string> { "are" } });
dict1.Add("01.1", new List<List<string>> { new List<string> { "just" }, new List<string> { "some" } });
dict1.Add("02", new List<List<string>> { new List<string> { "sample" }, new List<string> { "values" } });
var dict2 = new Dictionary<string, List<List<string>>>();
dict2.Add("1", new List<List<string>> { new List<string> { "01", "01.1" }, new List<string> { "foo", "bar" } });
dict2.Add("2", new List<List<string>> { new List<string> { "02", "value1" }, new List<string> { "02.21", "value2" } });
编辑4:
抱歉回复晚了。Gaurang Dave在评论中提出的建议和被接受的答案对我都有效。感谢大家的帮助!
02
的条目没有出现在结果中?这个关键字应该与dict2中关键字为2
的值相匹配啊! - Georg Patscheidervar result = dict2 .Where(d2 => d2.Key == selectedId) .Where(d2 => d2.Value.Any(d2lst => d2lst.Any(d2lst1 => dict1.Keys.Contains(d2lst1)))) .Select(d2 => d2.Value) .ToList();`
- Gaurang Dave