如何从嵌套字典中获取Lookup
本文关键字:获取 Lookup 字典 嵌套 | 更新日期: 2023-09-27 18:13:16
我有一个嵌套的字典,我想从中派生一个Lookup。字典的示例数据如下:
var binary_transaction_model = new Dictionary<string, Dictionary<string, bool>>();
binary_transaction_model.Add("123", new Dictionary<string, bool>(){{"829L", false},{"830L", true}});
binary_transaction_model.Add("456", new Dictionary<string, bool>(){{"829L", true},{"830L", false}});
binary_transaction_model.Add("789", new Dictionary<string, bool>(){{"829L", true},{"830L", true}});
这个LINQ语句正在工作:
var cols = from a in binary_transaction_model
from b in a.Value
where b.Value == true
group a.Key by b.Key;
它给我一个IEnumerable<IGrouping<String,String>>
。出于查找目的,我需要将此结果作为lookup数据结构。我该怎么做呢?ToLookup()
签名应该是什么样的?(编辑:我想Lookup<String,String>
。
应该可以:
var cols = (from a in binary_transaction_model
from b in a.Value
where b.Value == true
select new { aKey = a.Key, bKey = b.Key })
.ToLookup(x => x.bKey, x => x.aKey);