Darrel Hoffman
Darrel Hoffman

Reputation: 4636

Get a list of distinct property values from a list of objects

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.

Upvotes: 261

Views: 687509

Answers (7)

stefan
stefan

Reputation: 13

This works perfectly for me

List<string> ticketIds = new();
var uniqueTicketIds = ticketIds.Select(x => x).Distinct().ToList(); 

Upvotes: -1

Usman Jalil
Usman Jalil

Reputation: 192

@if (dataModal.Count > 0)
{
    var DistinctItems = dataModal.GroupBy(x => x.Year).Select(y => y.First());

    <div class="col-md-3">
        <label class="fs-7 form-label text-muted">@Localizer["Year"]</label>
            <InputSelect id="ddlYears" class="form-select" @bind-Value="filter.Year">                            
                <option value="">@Localizer["Select"]</option>
                @foreach (var year in DistinctItems)
                {
                   <option value="@year.Year">
                       @year.Year
                   </option>
                }
            </InputSelect>
    </div>
}

Upvotes: -1

Mohammad Atiour Islam
Mohammad Atiour Islam

Reputation: 5708

Distinct the Note class by Author

var DistinctItems = Notes.GroupBy(x => x.Author).Select(y => y.First());

foreach(var item in DistinctItems)
{
    //Add to other List
}

Upvotes: 133

Bhaskar
Bhaskar

Reputation: 49

public class KeyNote
{
    public long KeyNoteId { get; set; }
    public long CourseId { get; set; }
    public string CourseName { get; set; }
    public string Note { get; set; }
    public DateTime CreatedDate { get; set; }
}

public List<KeyNote> KeyNotes { get; set; }
public List<RefCourse> GetCourses { get; set; }    

List<RefCourse> courses = KeyNotes.Select(x => new RefCourse { CourseId = x.CourseId, Name = x.CourseName }).Distinct().ToList();

By using the above logic, we can get the unique Courses.

Upvotes: 3

Dan Busha
Dan Busha

Reputation: 3803

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

IEnumerable<Note> 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();

Upvotes: 49

ravinderreddy Seeelam
ravinderreddy Seeelam

Reputation: 35

mcilist = (from mci in mcilist select mci).Distinct().ToList();

Upvotes: -3

Kirk Woll
Kirk Woll

Reputation: 77536

Notes.Select(x => x.Author).Distinct();

This will return a sequence (IEnumerable<string>) of Author values -- one per unique value.

Upvotes: 477

Related Questions