[英]using LINQ in C# to convert a List<string> to a List<char>
I have a list of strings in C#, and want to create a list of unique characters that are in the strings in the list, using LINQ. 我在C#中有一个字符串列表,并希望使用LINQ创建列表中字符串中的唯一字符列表。
I have so far worked out how to turn the List into a List, but I can't work out how to get the LINQ to go further than that. 到目前为止,我已经找到了如何将List转换为List,但我无法弄清楚如何让LINQ更进一步。
What I have so far is as follows: 我到目前为止的内容如下:
List<string> dictionary = new List<string>(someArray);
List<string[]> uniqueCharacters = dictionary.ConvertAll(s => s.Split());
I believe I need to something along the lines of 我相信我需要一些东西
List<char> uniqueCharacters =
dictionary.ConvertAll(s => s.Split()).SelectAll(t, i=>t[i][0]);
You can use LINQ's SelectMany method, eg: 您可以使用LINQ的SelectMany方法,例如:
var list = new List<string> { "Foo", "Bar" };
var chars = list.SelectMany(s => s.ToCharArray());
var distinct = chars.Distinct();
Get your LinQ result and put it in loop, compare every char with in list of char. 获取您的LinQ结果并将其放入循环中,将每个char与char列表进行比较。
foreach (string character in dictionary)
{
if (!(uniqueCharacters).Contains(character))
{
uniqueCharacters.Add(character);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.