use*_*222 0 c# linq dictionary
我有SortedDictionary<int, Dictionary<string, HashSet<long>>>
如何使用 Linq 展平结果?
我试过了:
var result = dictionary.Values.SelectMany(x => x);
Run Code Online (Sandbox Code Playgroud)
但我怎样才能压平到最底部呢?
我需要收集int, string, HashSet<long>
您可以使用类似以下内容:
var flattened = dictionary.SelectMany(x => x.Value.Select(y => (x.Key, y.Key, y.Value)));
Run Code Online (Sandbox Code Playgroud)
这将产生一个 IEnumerable ValueTyple<int, string, HashSet<long>>。
请注意,因为这里的类型是IEnumerable,所以它还没有具体化。.ToList()您可能希望在末尾添加 a (在这种情况下,类型将为List<ValueTyple<int, string, HashSet<long>>>)或将其转换为您想要的任何其他集合类型。