gpt4 book ai didi

c# - 带有 Linq 的 JArray 中的 JSON.NET 多个 orderby

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:07 25 4
gpt4 key购买 nike

我有以下 JSON,我想知道是否可以使用 Linq 执行多个 OrderBy

var body = @"[{                            
""portOfLoading"": ""GOT"",
""bookingResponses"":[{
""bookingNumber"": ""11"",
""comment"": ""LOFO"",
""customerReference"": ""3423462"",
""departureDate"": ""2017-04-10"",
""departureTime"": ""18:00"",
""description"": ""desc"",
""length"": ""7482"",
""netWeight"": ""12345"",
""plugin"": ""true"",
""resourceCode"": ""CONT26"",
""route"": ""GOTZEE"",
""status"": ""Price missing"",
""unitNumber"": ""ABC123"",
""width"": ""0""
}
]
}
,
{

""portOfLoading"": ""GOT"",
""bookingResponses"":[{
""bookingNumber"": ""3"",
""comment"": ""LOFO"",
""customerReference"": ""3423462"",
""departureDate"": ""2017-04-10"",
""departureTime"": ""18:00"",
""description"": ""desc"",
""length"": ""7482"",
""netWeight"": ""12345"",
""plugin"": ""true"",
""resourceCode"": ""CONT26"",
""route"": ""GOTZEE"",
""status"": ""Price missing"",
""unitNumber"": ""ABC123"",
""width"": ""0""
}
]
}
,{
""portOfLoading"": ""OUL"",
""bookingResponses"":[{
""bookingNumber"": ""7"",
""comment"": ""STANDBY"",
""customerReference"": ""3423462"",
""departureDate"": ""2017-04-10"",
""departureTime"": ""18:00"",
""description"": ""desc"",
""length"": ""7482"",
""netWeight"": ""12345"",
""plugin"": ""true"",
""resourceCode"": ""CONT26"",
""route"": ""OULZEE"",
""status"": ""Price missing"",
""unitNumber"": ""ABC123"",
""width"": ""0""
}
]
},{
""portOfLoading"": ""ZEE"",
""bookingResponses"":[{
""bookingNumber"": ""3"",
""comment"": ""STANDBY"",
""customerReference"": ""3423462"",
""departureDate"": ""2017-04-10"",
""departureTime"": ""18:00"",
""description"": ""desc"",
""length"": ""7482"",
""netWeight"": ""12345"",
""plugin"": ""true"",
""resourceCode"": ""CONT26"",
""route"": ""ZEEGOT"",
""status"": ""Price missing"",
""unitNumber"": ""ABC123"",
""width"": ""0""
}
]
},{
""portOfLoading"": ""GOT"",
""bookingResponses"":[{
""bookingNumber"": ""10"",
""comment"": ""STANDBY"",
""customerReference"": ""3423462"",
""departureDate"": ""2017-04-10"",
""departureTime"": ""18:00"",
""description"": ""desc"",
""length"": ""7482"",
""netWeight"": ""12345"",
""plugin"": ""true"",
""resourceCode"": ""CONT26"",
""route"": ""GOTZEE"",
""status"": ""Price missing"",
""unitNumber"": ""ABC123"",
""width"": ""0""
}
]
}
]";

到目前为止,我已经得到了“第一个”orderby 的工作,就像这样:

JArray jsonVal = JArray.Parse(body);
JArray sortQuery = new JArray(jsonVal.OrderBy(obj => obj["portOfLoading"]));

"portOfLoading" 之后,我想按 "bookingNumber" 订购。我试过使用 ThenBy 等等,但从未成功。谢谢

最佳答案

如果 "bookingResponses" 中始终有一个项目,如您的示例所示,请执行以下操作:

JArray jsonVal = JArray.Parse(body);
JArray sortQuery = new JArray(jsonVal.OrderBy(obj => obj["portOfLoading"])
.ThenBy(obj => int.Parse(obj["bookingResponses"].FirstOrDefault()?["bookingNumber"].ToString())));

添加 Int.Parse 的原因是因为如果没有它,"bookingNumber" 将按其文本顺序(作为字符串)而不是数字顺序进行排序。导致顺序为:1,10,11,3。如果不确定这些值是否始终是有效整数(并因此导致 InvalidCastException),可以执行类似 this answer 中的操作

关于c# - 带有 Linq 的 JArray 中的 JSON.NET 多个 orderby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43474485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com