2013-09-23 126 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(); 

所以它现在确定:)