LINQ GroupBy根据List C#里面的一个键里面的字典#

时间:2013-09-23 13:28:40

标签: c# linq dictionary

我正在创建一个包含字典词典的列表,用于从wcf服务创建Json。

即时创建:

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

我用数据填充它,Json看起来像这样:

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

起始数组是List

第一个Object是词典词典 并且第一个字典中的对象再次是具有键/值对字符串/对象的字典 (我使用object,因为类型可能是bool或int或string) 现在,最后一级的字典看起来像这样:

   "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但不工作或不知道如何:s

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

如果我创建新列表或更改现有列表,则可以。

1 个答案:

答案 0 :(得分: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>();

然后我填充列表中的对象,然后使用额外的double total_price我将它们排序为:

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

所以现在好了:)