JSON.NET与Linq的JArray中的多个orderby

问题描述:

我有以下的JSON,我想知道是否有可能使用Linq做多个OrderByJSON.NET与Linq的JArray中的多个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等,但从来没有得到它的工作。由于

+0

如果''bookingResponses'''在其下面有几个项目会怎么样?你的预期产出是多少? –

+0

你可以使用'.ThenBy(obj2 => obj2 [“bookingNumber”]'我想。 – Achilles

+0

@Achilles你不能,看吉拉德的答案为什么这是不够的。 –

如果"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