Inspector和InspectorRates有两个通用列表。
RateType有三个不同的值(0 =不选择,1 =日费率,2 =每小时费率)。
我想首先向所有检查员显示日间类型费率,然后显示最低费率。如果用户选择“每小时费率”选项,则列表需要按小时费率和最低费率排序。没有选择的费率总是在底部。
我尝试过LINQ,但它没有用。
listI.OrderBy(Function(i) i.DefaultRate.RateType = Rates.RateTypeEnum.Day_Rate).ThenBy(Function(i) i.DefaultRate.Rate)
答案 0 :(得分:1)
您可以使用OrderBy
和ThenBy
来提供基于优先级的搜索条件
List<Inspector> list = new List<Inspector>();
list.Add(new Inspector() { RateType = 0, Rates = 0 });
list.Add(new Inspector() { RateType = 0, Rates = -1 });
list.Add(new Inspector() { RateType = 1, Rates = 1 });
list.Add(new Inspector() { RateType = 1, Rates = -2 });
list.Add(new Inspector() { RateType = 1, Rates = 3 });
list.Add(new Inspector() { RateType = 2, Rates = 9 });
list.Add(new Inspector() { RateType = 2, Rates = -2 });
var sortedList = list
.OrderByDescending(i => i.RateType == 1)
.ThenBy(i => i.Rates).ToList();
输出:
//RateType = 1, Rates = -2
//RateType = 1, Rates = 1
//RateType = 1, Rates = 3
//RateType = 2, Rates = -2
//RateType = 0, Rates = -1
//RateType = 0, Rates = 0
//RateType = 2, Rates = 9
以下是Inspector
类定义:
public class Inspector
{
public int RateType { get; set; }
public int Rates { get; set; }
public int InspectorId { get; set; }
public override string ToString()
{
return string.Format("Type:{0}, Rate:{1}", RateType, Rates);
}
}