Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JArray中JSON.NET多个orderby和Linq_C#_Arrays_Json_Linq_Json.net - Fatal编程技术网

C# JArray中JSON.NET多个orderby和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

我有下面的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""
            }
            ]
        }
    ]";
到目前为止,我已经有了第一批订单,如下所示:

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的答案以了解为什么这不够。