bigbang
bigbang

Reputation: 103

How to get a Lookup from nested dictionaries

I have a nested dictionary from which I want to derive a Lookup. Example data of the dictionary is following:

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

This LINQ statement is working:

var cols = from a in binary_transaction_model
    from b in a.Value
    where b.Value == true
    group a.Key by b.Key;

It gives me an IEnumerable<IGrouping<String,String>>. For lookup purposes I need this result as a Lookup data structure. How can I do this? How should a ToLookup() signature look like? (Edit: I want to have a Lookup<String,String>.)

Upvotes: 0

Views: 475

Answers (1)

digEmAll
digEmAll

Reputation: 57210

This should work:

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

Upvotes: 2

Related Questions