我在C#中有一个字符串列表,并希望使用LINQ创建列表中字符串中的唯一字符列表.
到目前为止,我已经找到了如何将List转换为List,但我无法弄清楚如何让LINQ更进一步.
我到目前为止的内容如下:
List<string> dictionary = new List<string>(someArray);
List<string[]> uniqueCharacters = dictionary.ConvertAll(s => s.Split());
Run Code Online (Sandbox Code Playgroud)
我相信我需要一些东西
List<char> uniqueCharacters =
dictionary.ConvertAll(s => s.Split()).SelectAll(t, i=>t[i][0]);
Run Code Online (Sandbox Code Playgroud) 从列表中删除重复值的最佳算法是什么?我试过这个:
for (int i = 0; i < AuthorCounter-1; i++)
{
for (int j = 0; j < AuthorCounter-1; j++)
{
if (i != j)
{
if (AuthorGroupNode.Nodes[i].Text == AuthorGroupNode.Nodes[j].Text)
{
AuthorGroupNode.Nodes[j].Remove();
AuthorCounter--;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这AuthorGroupNodes是节点上的列表.它在某种程度上做得对,但并不完美.谁有更好的解决方案???
这在某种程度上与此(获取 C# 列表中的所有唯一项)问题有关。
上面的问题是在谈论一个简单的值数组。我有一个从第三方 Web 服务返回的对象:
public class X
{
public Enum y {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我有这些对象的列表,List<x> data;总共大约 100 条记录但可变。现在我想要属性列表中的所有可能值,y我想绑定它做一个CheckBoxList.DataSource(以防万一)。
如何最有效的方法来做到这一点?
我可以想到两种算法:
var data = HashSet<Enum> hashSet = new HashSet<Enum>(xs.Select(s => s.y));
chkBoxList.DataSource = data;
Run Code Online (Sandbox Code Playgroud)
或者
var data = xs.Select(s => s.y).Distinct();
chkBoxList.DataSource = data;
Run Code Online (Sandbox Code Playgroud)
我的直觉是 HashSet 但我不是 100% 确定。
如果有人有任何想法,可以接受更好的想法吗?
c# ×4
list ×3
linq ×2
algorithm ×1
c#-4.0 ×1
duplicates ×1
generics ×1
hashset ×1
performance ×1