問題:
In C#, say I have a class called Note
with three string member variables.在 C# 中,假設我有一個名爲Note
的類,其中包含三個字符串成員變量。
public class Note
{
public string Title;
public string Author;
public string Text;
}
And I have a list of type Note
:我有一個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?在 Author 列中獲取所有不同值的列表的最簡潔方法是什麼?
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.我有一種感覺,有一些神奇的 Linq 結構可以在一行中做到這一點,但我一直想不出任何東西。
解決方案:
參考一: https://en.stackoom.com/question/h1pB參考二: https://stackoom.com/question/h1pB