Reputation: 1402
I have this list.I want to have distinct values based on category sorted by the Order .Can someone please tell me how can I do it.
public class Item
{
public string URL{ get; set; }
public string Title { get; set; }
public string Category { get; set; }
public string Order{ get; set; }
}
var Test = new List<Items>
var groups = Test.Select(g => g.category).Distinct();
Thanks
Upvotes: 0
Views: 80
Reputation: 20777
var groups = Test
.GroupBy(g => g.Category)
.Select(g => new
{
Category = g.Key,
Items = g.OrderBy(i => i.Order)
})
.OrderBy(g => g.Category)
.SelectMany(g => g.Items)
.ToList();
or, essentially the same thing in this case:
Test.OrderBy(it => it.Category).ThenBy(it => it.Order)
The first code you can take out the SelectMany if you want a list of groups with sub-lists of items in that category sorted by Order.
Upvotes: 1