Wednesday, December 22, 2021

c# - How to make generic unique list with GroupBy()

This code make unique book name list + ensures sorting of the list.
private List notes = new List();
...
List uniqueBooks = new List();

uniqueBooks = 
  notes.GroupBy( x => x.BookName ).Select( g => new Note { BookName = g.Key } ).ToList();
uniqueBooks.Sort( (x, y ) => x.BookName.CompareTo( y.BookName ) );

No comments:

Post a Comment