LINQ GroupBy一个列表根据一个键里面的字典里面的字典里面List C# [英] LINQ GroupBy a List according to a key inside dictionary inside dictionary inside List C#

查看:436
本文介绍了LINQ GroupBy一个列表根据一个键里面的字典里面的字典里面List C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建一个包含字典的字典,用于从wcf服务创建Json。

 列表<字典<字符串,字典< string,Object>>> superduperList = new List< Dictionary< string,Dictionary< string,Object>>>(); 

我填写数据,Json看起来像这样:

  [
{
DepartureJ:{},
ReturnJ:{},
priceDepartureJ:{},
priceReturnJ:{},
DepartureSegmentsJ:{},
ArrivalSegmentsJ:{}
},
...,
...,
...,
...,
...,
]

起始数组是列表



第一个对象是词典词典
,第一个词典中的对象再次是具有键/价值对字符串/对象
(我使用对象,因为类型可以是bool或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:不可退款,
FareBasisCode:xxx-,
RoutingId :,
GroupCompStr:ATH-SKG-1xxxxxx-UOWA3--0,
LineCompStr:-2013-09xxxxxxxxxxxxxxxxxxxA3--3,0000000-1-0--,
TotalAmount_From_Prices:136.64
}

现在我的问题是我如何排序外部列表键值TotalAmount_From_Prices位于列表的每个项目的每个字典的字典内?



我尝试与groupby与LINQ但不工作或不知道如何:

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

如果我创建一个新列表或更改现有的

解决方案

实际上我用不同的方式。



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

  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 =新词典< string,object>();
public Dictionary< string,object> pricesReturnJ =新词典< string,object>();
public Dictionary< string,object> DepartureSegmentsJ =新词典< string,object>();
public Dictionary< string,object> ArrivalSegmentsJ =新词典< string,object>();

public double total_price;

}

然后我创建:

 列表< each_flight>航班=新列表< each_flight>(); 

然后我填充列表中的对象,然后加上额外的double total_price,我按照这样排序:

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

所以它现在好了:)


I am Creating a list that has dictionaries of dictionaries for creating Json with it from a wcf service.

im creating like this:

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

Im filling it with data and the Json Looks like this:

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

the starting Array is the List

the first Object is the Dictionary of Dictionaries and the Objects Within the First Dictionary are again Dictionaries with Key/Value Pairs String/object (i use object because the type could be bool or int or string) now a dictionary in the last level looks like this:

   "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
    }

Now My Question is how do i Sort the Outer List From the Key TotalAmount_From_Prices which lies inside the dictionary of each dictionary of each item of the list?

i tried with groupby with LINQ but not working or don't know how :s

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

its ok if i create a new list or change the existing.

解决方案

Well actually i did it in a different manner.

I created a custom Class to hold the data as following:

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;

}

and then i created:

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

and then i populated the objects in the list and then with the extra double total_price i sorted them like this:

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

so its ok now :)

这篇关于LINQ GroupBy一个列表根据一个键里面的字典里面的字典里面List C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆