Get a list of distinct values in List

后端 未结 5 1289
孤城傲影
孤城傲影 2020-11-27 12:01

In C#, say I have a class called Note with three String member variables.

public class Note
{
    public string Title;
    public string Author;
    public s         


        
5条回答
  •  一生所求
    2020-11-27 12:16

    Jon Skeet has written a library called morelinq which has a DistinctBy() operator. See here for the implementation. Your code would look like

    IEnumerable distinctNotes = Notes.DistinctBy(note => note.Author);
    

    Update: After re-reading your question, Kirk has the correct answer if you're just looking for a distinct set of Authors.

    Added sample, several fields in DistinctBy:

    res = res.DistinctBy(i => i.Name).DistinctBy(i => i.ProductId).ToList();
    

提交回复
热议问题