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



是否有一种方法来排序对象列表,通过它的键/值对的字典?通过尊重动态名称列表的顺序。在本例中,它将是["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é"
},
],
}
]

我可以按游戏和系列进行排序。

Lprod.OrderBy (x =祝辞x.gamme) .ThenBy (x =祝辞x.serie)

但是现在我被最后一个条件困住了,就像我说的,它是动态的。那么有没有办法通过传递string

来排序这个对象列表呢?

["Condition1","Condition2","Condition3","Condition4"]

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

相关内容

  • 没有找到相关文章

最新更新