Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order the item categories in linq

I am having 20 records

I want to order the records in terms of Priority field.

Priority field is having 3 values( High, Medium, Low )

I want to display the records in High, Medium, Low records in sequence respectively.

var groupedRecords = records.GroupBy(x => x.Priority == "High") // confused here...

Note: I want to get first all High records, then Medium records and atlast Low records.

like image 634
StackOverflow Avatar asked Jan 07 '23 03:01

StackOverflow


1 Answers

Why not map the priorities to, say, integers:

  High   -> 1
  Medium -> 2
  Low    -> 3 

So it can be something like this:

  var groupedRecords = records.
    OrderBy(x => (x.Priority == "High") 
      ? 1 
      : (x.Priority == "Medium") ? 2 : 3);
like image 120
Dmitry Bychenko Avatar answered Jan 09 '23 17:01

Dmitry Bychenko