简体   繁体   English

从嵌套集合中收集键值的独特列表?

[英]Collect a distinct list of key values from a nested collection?

This seems like it is completely straightforward, but my brain isn't working very well this morning, and I can't seem to find a good search expression for the answer. 这似乎很简单,但是今天早上我的大脑工作得不太好,而且我似乎找不到很好的搜索表达式作为答案。

I have an IList<IDictionary<string, string>> and I want to get an IList<string> containing a distinct list of unique key names in my collection of dictionary objects. 我有一个IList<IDictionary<string, string>>并且我想获取一个IList<string> ,该IList<string>在我的字典对象集合中包含唯一键名称的不同列表。 What's a quick and elegant way to do this? 什么是快速而优雅的方法? I'm sure the answer is simple, I just can't think of it. 我敢肯定,答案很简单,我想不出来。

var allKeys = from dict in list
              from key in dict.Keys
              select key;

var distinctKeys = allKeys.Distinct().ToList();

Or if you prefer lambda syntax: 或者,如果您更喜欢lambda语法:

var distinctKeys = list.SelectMany(dict => dict.Keys).Distinct().ToList();
list.SelectMany(d => d.Keys).Distinct()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM