【问题标题】:JSON.NET multiple orderby in JArray with Linq使用 Linq 在 JArray 中的 JSON.NET 多个 orderby
【发布时间】:2017-04-18 14:05:46
【问题描述】:

我有以下 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" 下面有多个项目怎么办?你的预期输出是什么?
  • 我想你可以使用.ThenBy(obj2 => obj2["bookingNumber"]
  • @Achilles 你不能,请参阅 Gilad 的回答,了解为什么这还不够。

标签: c# arrays json linq json.net


【解决方案1】:

如果"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 中的类似操作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多