2014-10-03 28 views
1

檢查器和InspectorRates有兩個通用列表。按通用對象的多個值和特定值排列通用對象的通用列表

RateType有三個不同的值(0 =未選擇,1 =日費率,2 =小時費率)。

我想向所有檢查員顯示日類型費率,然後是最低費率。如果用戶選擇「小時費率」選項,則列表需要按小時費率和最低費率排序。未選擇的費率將始終處於最低點。

enter image description here

enter image description here

我曾嘗試LINQ,但它不工作。

listI.OrderBy(Function(i) i.DefaultRate.RateType = Rates.RateTypeEnum.Day_Rate).ThenBy(Function(i) i.DefaultRate.Rate) 
+0

RateType 1之後,應該下一個? – 2014-10-03 15:28:27

+0

我已更新我的問題,我不認爲它是重複的。 – user1263981 2014-10-06 15:29:53

回答

1

您可以使用OrderByThenBy提供基於優先級的搜索條件

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); 
    } 
} 
+0

備註:「這可能對你有幫助」並未在答案中添加任何信息。要麼只有代碼(這是可以的),要麼提供一些描述/鏈接。對於這個特定的問題,建議重複將是更好的方法,顯然這不是第一次嘗試按2個屬性對列表進行排序。 – 2014-10-03 15:53:37

+0

我按2屬性排序,但需要按特定值排序第一個屬性。 – user1263981 2014-10-03 15:56:39

+0

@ user1263981考慮編輯你的問題,並做一些數值例子(輸入和期望的輸出)來澄清你的意思。 – 2014-10-03 15:58:19