如何从嵌套字典中获取查找

时间:2013-05-13 15:38:14

标签: c# linq lookup

我有一个嵌套字典,我想从中派生一个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>

1 个答案:

答案 0 :(得分:2)

这应该有效:

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);