获取列表不同值的列表 [英] Get a list of distinct values in List
问题描述
在C#中,说我有三个字符串成员变量的类名为注意:
公共类注意事项
{
公共字符串称号;
公共字符串作者;
公共字符串文本;
}
和我有型注的列表:
列表<注意>注=新的List<注意>();
什么是获得在作者列中所有不同值的列表最彻底的方法?
我可以遍历列表,并添加不重复为字符串的另一个列表中的所有值,但这似乎脏,效率低下。我有一种感觉,有一些神奇的LINQ的建设将在一行做到这一点,但我一直没能拿出任何东西。
Notes.Select(X => x.Author).Distinct();
这将返回一个序列(的IEnumerable<串>
)作者
值 - 每独特的价值之一。
In C#, say I have a class called Note with three String member variables.
public class Note
{
public string Title;
public string Author;
public string Text;
}
And I have a list of type Note:
List<Note> Notes = new List<Note>();
What would be the cleanest way to get a list of all distinct values in the Author column?
I could iterate through the list and add all values that aren't duplicates to another list of strings, but this seems dirty and inefficient. I have a feeling there's some magical Linq construction that'll do this in one line, but I haven't been able to come up with anything.
Notes.Select(x => x.Author).Distinct();
This will return a sequence (IEnumerable<string>
) of Author
values -- one per unique value.
这篇关于获取列表不同值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!