我有一个C#字符串列表,想要使用LINQ创建一个只包含这些字符串中唯一的字符的列表。
我已经将List转换为List,但我无法弄清楚如何使LINQ进一步操作。
目前我所拥有的代码如下:
List<string> dictionary = new List<string>(someArray);
List<string[]> uniqueCharacters = dictionary.ConvertAll(s => s.Split());
我相信我需要做一些类似于以下的事情:
List<char> uniqueCharacters =
dictionary.ConvertAll(s => s.Split()).SelectAll(t, i=>t[i][0]);
HashSet
在结尾处var distinct = new HashSet(chars)
是另一种合理的选择。 参见https://dev59.com/a3M_5IYBdhLWcg3waSb9 - Ian MercerToCharArray
调用是不必要的,并且对性能影响很小,因为字符需要从源字符串复制到新数组中。您可以使用list.SelectMany(s => s)
代替。 - LukeH