It sounds like you just need a combination of SelectMany
, Select
and Distinct
:
var allKeys = sourceData.SelectMany(list => list) // Flatten to a sequence of KVP
.Select(kvp => kvp.Key) // Select just the keys
.Distinct();
Note that if you want to iterate over allKeys
more than once, you probably want to materialize the query, e.g. by calling ToList
at the end:
var allKeys = sourceData.SelectMany(list => list) // Flatten to a sequence of KVP
.Select(kvp => kvp.Key) // Select just the keys
.Distinct()
.ToList();