简体   繁体   English

如何根据键值对的字典对对象列表进行排序

[英]How to sort a list of objects depending on it's dictionary of key,value pair

Is there a way to sort a list of objects, by it's dictionary of key/value pairs ?有没有办法通过键/值对字典对对象列表进行排序? By respecting the order of a dynamic list of names.通过尊重动态名称列表的顺序。 for this example it's going to be ["Condition1","Condition2","Condition3","Condition4"]对于这个例子,它将是 ["Condition1","Condition2","Condition3","Condition4"]

[
          { 
        "uid": "R-T-X",
        "gamme": "MLOP",
        "serie": "XENA",
        "description": "450 - 450 - 8.8 -  - Blanco - / - U3 P3 E3 C2",
        "caracteristiques": [
            {
                "name": "Condition1",
                "value": "450"
            },
            {
                "name": "Condition2",
                "value": "450"
            },
            {
                "name": "Condition3",
                "value": "8.8"
            },
            {
                "name": "Condition4",
                "value": "Non rectifié"
            },
        ],
    },
    {

        "uid": "L-M-T",
        "gamme": "MLOP",
        "serie": "XENA",
        "description": "600 - 600 - 9.6 -  - Blanco - / - U4 P4 E3 C2",
        "caracteristiques": [
            {
                "name": "Condition1",
                "value": "600"
            },
            {
                "name": "Condition2",
                "value": "600"
            },
            {
                "name": "Condition3",
                "value": "9.6"
            },
            {
                "name": "Condition4",
                "value": "Rectifié"
            },
        ],
    },
    {
        "uid": "M-T-F",
        "gamme": "MLOP",
        "serie": "XENA",
        "description": "750 - 750 - 9.8 -  - Acero - / - U4 P3 E3 C2",
        "caracteristiques": [
           {
                "name": "Condition1",
                "value": "750"
            },
            {
                "name": "Condition2",
                "value": "750"
            },
            {
                "name": "Condition3",
                "value": "9.8"
            },
            {
                "name": "Condition4",
                "value": "Rectifié"
            },
        ],
    }
 ]

I was able to sort it by gamme and serie.我能够按游戏和意甲对其进行排序。

Lprod.OrderBy(x=>x.gamme).ThenBy(x=>x.serie) Lprod.OrderBy(x=>x.gamme).ThenBy(x=>x.serie)

But right now I'm stuck with the last condition that as I said will be dynamic.但是现在我坚持最后一个条件,正如我所说的那样是动态的。 So Is there a way to Order this list of objects by passing a list of string那么有没有办法通过传递字符串列表来排序这个对象列表

["Condition1","Condition2","Condition3","Condition4"] ["条件1","条件2","条件3","条件4"]

Assume caracteristiques is deserialized as a Dictionary<string, string> , I believe you want something like this:假设caracteristiques被反序列化为Dictionary<string, string> ,我相信你想要这样的东西:

var conds = ["Condition1","Condition2","Condition3","Condition4"];
var q = Lprod.OrderBy(x=>x.gamme).ThenBy(x=>x.serie);
foreach(var c in conds)
{
    q = q.ThenBy(x=>x.caracteristiques[c]);
}
var output = q.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM