我有20条记录
我想按照优先级
字段对记录进行排序。
优先级
字段有3个值(高,中,低
)
我想依次显示高,中,低
的记录。
var groupedRecords = records.GroupBy(x => x.Priority == "High") // confused here...
注意:我希望先获取高记录,然后是中等记录,最后是低记录。
为什么不将优先级映射成整数呢:
High -> 1
Medium -> 2
Low -> 3
var groupedRecords = records.
OrderBy(x => (x.Priority == "High")
? 1
: (x.Priority == "Medium") ? 2 : 3);
你希望对列表进行排序,因此可以使用OrderBy
或OrderByDescending
扩展方法。
var orderedRecords = records.OrderByDescending(x => x.Priority == "High").ThenByDescending(x => x.Priority == "Medium");
Predicate<T>
本质上是 Func<T,bool>
,所以这段代码可以编译通过。 - kkyr请尝试以下:
var groupedRecords = records.OrderBy(x => x.Priority == "High").ThenBy(x => x.Priority == "Medium").ThenBy(x => x.Priority == "Low");
一种方法是
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());