如何在linq中订购商品类别

Sta*_*low 4 .net c# linq asp.net c#-4.0

我有20条记录

我想按Priority字段排序记录.

Priority字段有3个值(High, Medium, Low)

我想High, Medium, Low分别按顺序显示记录中的记录.

var groupedRecords = records.GroupBy(x => x.Priority == "High") // confused here...
Run Code Online (Sandbox Code Playgroud)

注:我想首先所有的高记录,然后中记录和atlast低记录.

Dmi*_*nko 6

为什么不将优先级映射到整数:

  High   -> 1
  Medium -> 2
  Low    -> 3 
Run Code Online (Sandbox Code Playgroud)

所以它可以是这样的:

  var groupedRecords = records.
    OrderBy(x => (x.Priority == "High") 
      ? 1 
      : (x.Priority == "Medium") ? 2 : 3);
Run Code Online (Sandbox Code Playgroud)