首页 文章

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

提问于
浏览
1

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

即时创建如下:

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

我填充数据和Json看起来像这样:

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

起始数组是List

第一个对象是词典词典,第一个词典中的对象再次是带有键/值对的字典串/对象(我使用对象,因为类型可能是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

    实际上,我以不同的方式做到了 .

    我创建了一个自定义类来保存数据,如下所示:

    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();
    

    所以现在好了:)

相关问题