unique() function. The unique() function is used to find the unique elements of an array. Returns the sorted unique elements of an array.
Notes.Select(x => x.Author).Distinct();
This will return a sequence (IEnumerable<string>
) of Author
values -- one per unique value.
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
}
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();
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 Course
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With