2013-09-23 173 views
1

我正在創建一個包含字典字典的列表,用於從wcf服務創建Json。LINQ GroupBy根據字典裏面的字典裏面的一個關鍵字List裏面的字典裏面#

即時創建這樣的:

List<Dictionary<string, Dictionary<string, Object>>> superduperList = new List<Dictionary<string, Dictionary<string, Object>>>(); 

林填充數據,並將JSON看起來像這樣:

[ 
{ 
DepartureJ: {}, 
ReturnJ: {}, 
pricesDepartureJ: {}, 
pricesReturnJ: {}, 
DepartureSegmentsJ: {}, 
ArrivalSegmentsJ: {} 
}, 
..., 
..., 
..., 
..., 
..., 
] 

起始陣列是List

所述第一對象是字典 和第一個字典中的對象再次是帶鍵/值對的字典字符串/對象 (i使用對象,因爲類型可以是布爾或INT或字符串) 現在在最後一個級別的字典是這樣的:

"DepartureJ": { 
     ArrivalDateTime: "2013-09-27T12:15:00", 
     ArrivalDateTime_str: "12:15", 
     StopQuantity: 0, 
     StopQuantity_str: "Direct", 
     TotalDuration: "50", 
     TotalDuration_str: "0h 50mins", 
     SeatsRemaining_str: "2", 
     NoBag_str: "", 
     NonRefundable: true, 
     NonRefundable_str: "Non Refundable", 
     FareBasisCode: "xxx-", 
     RoutingId: "", 
     GroupCompStr: "ATH-SKG-1xxxxxx-UOWA3--0", 
     LineCompStr: "-2013-09xxxxxxxxxxxxxxxxxxxxxA3--3,0000000-1-0--", 
     TotalAmount_From_Prices: 136.64 
    } 

現在我的問題是我怎麼排序外列表,從中在於裏面的關鍵TotalAmount_From_Prices列表中每個項目的每個字典的字典?

我試圖與GROUPBY與LINQ但不工作或者不知道如何:■

superduperList.GroupBy(each_result=> each_result["DepartureJ"]["TotalAmount_From_Prices"]); 

其確定如果我創建一個新的列表或改變現有的。

+0

你只是在嘗試對內部字典進行排序還是需要對外部字典進行排序?含義...如果outer包含Dict1和Dict2,Dict1包含TotalAmount val 1,2,Dict 2包含val 3,1 ...結果應該是什麼,只是包含排序的Dict1(1,2)和Dict( 1,3)還是你試圖對外部字典進行排序? –

+0

我想僅對外部列表進行排序,但取決於內部/內部Dict的鍵和字典是否具體 – aimiliano

+0

您的字典包含對象,因此您需要將該值轉換爲double。您還需要確保在將json解析到數據結構中時將其存儲爲double。 –

回答

0

其實我以不同的方式做到了。

我創建了一個自定義的類來保存數據如下:

public class each_flight 
{ 
    public Dictionary<string, object> DepartureJ = new Dictionary<string, object>(); 
    public Dictionary<string, object> ReturnJ = new Dictionary<string, object>(); 
    public Dictionary<string, object> pricesDepartureJ = new Dictionary<string, object>(); 
    public Dictionary<string, object> pricesReturnJ = new Dictionary<string, object>(); 
    public Dictionary<string, object> DepartureSegmentsJ = new Dictionary<string, object>(); 
    public Dictionary<string, object> ArrivalSegmentsJ = new Dictionary<string, object>(); 

    public double total_price; 

} 

然後我創建:

List<each_flight> flights = new List<each_flight>(); 

,然後我居住在列表中的對象,然後用額外的雙total_price我排序他們這樣:

List<each_flight> Fligths_sorted = flights.OrderBy(o => o.total_price).ToList(); 

所以它現在確定:)