2017-04-18 91 views
1

我有以下的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你不能,看吉拉德的答案爲什麼這是不夠的。 –

回答

2

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