您可以使用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);
}
}
RateType 1之後,應該下一個? – 2014-10-03 15:28:27
我已更新我的問題,我不認爲它是重複的。 – user1263981 2014-10-06 15:29:53