C# JArray中JSON.NET多个orderby和Linq
我有下面的JSON,我想知道是否可以使用Linq执行多个C# JArray中JSON.NET多个orderby和Linq,c#,arrays,json,linq,json.net,C#,Arrays,Json,Linq,Json.net,我有下面的JSON,我想知道是否可以使用Linq执行多个OrderBy var body = @"[{ ""portOfLoading"": ""GOT"", ""bookingResponses"":[{ ""bookingNumber"": ""11"", ""comment"": ""LOFO"", ""cu
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""
}
]
}
]";
到目前为止,我已经有了第一批订单,如下所示:
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
),可以执行类似于中的操作,如果“bookingResponses”
下面有多个项目呢?您的预期输出是什么?您可以使用。然后(我想是obj2=>obj2[“bookingNumber”]
。@Achiles您不能,请参阅Gilad的答案以了解为什么这不够。