How to order product categories in linq

I have 20 entries

I want to order entries in terms of a field Priority.

Field

Priorityhas 3 values ​​( High, Medium, Low)

I want to display records in High, Medium, Lowrecords in sequence accordingly.

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

Note. I want to get first all the high recording, and the average recording and atlast low record.

+4
source share
4 answers

You want to order a list, so use the OrderByor extension OrderByDescending.

var orderedRecords = records.OrderByDescending(x => x.Priority == "High").ThenByDescending(x => x.Priority == "Medium");
+2
source

, , :

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

- :

  var groupedRecords = records.
    OrderBy(x => (x.Priority == "High") 
      ? 1 
      : (x.Priority == "Medium") ? 2 : 3);
+6

:

var groupedRecords = records.OrderBy(x => x.Priority == "High").ThenBy(x => x.Priority == "Medium").ThenBy(x => x.Priority == "Low");
0

-

 var result = records.Where(x => x.Priority == "High")
                     .ToList()
                     .AddRange(records.Where(x => x.Priority == "Medium").ToList())
                     .AddRange(records.Where(x => x.Priority == "Low").ToList());
0

Source: https://habr.com/ru/post/1617028/


All Articles