diff --git a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs index 5b5af163..5cfb2b46 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs @@ -298,5 +298,60 @@ Parameter name: index")] string json = @"{""prop"":""value""}"; JArray.Parse(json); } + + public class ListItemFields + { + public string ListItemText { get; set; } + public object ListItemValue { get; set; } + } + + [Test] + public void ArrayOrder() + { + string itemZeroText = "Zero text"; + + IEnumerable t = new List + { + new ListItemFields { ListItemText = "First", ListItemValue = 1 }, + new ListItemFields { ListItemText = "Second", ListItemValue = 2 }, + new ListItemFields { ListItemText = "Third", ListItemValue = 3 } + }; + + JObject optionValues = + new JObject( + new JProperty("options", + new JArray( + new JObject( + new JProperty("text", itemZeroText), + new JProperty("value", "0")), + from r in t + orderby r.ListItemValue + select new JObject( + new JProperty("text", r.ListItemText), + new JProperty("value", r.ListItemValue.ToString()))))); + + string result = "myOptions = " + optionValues.ToString(); + + Assert.AreEqual(@"myOptions = { + ""options"": [ + { + ""text"": ""Zero text"", + ""value"": ""0"" + }, + { + ""text"": ""First"", + ""value"": ""1"" + }, + { + ""text"": ""Second"", + ""value"": ""2"" + }, + { + ""text"": ""Third"", + ""value"": ""3"" + } + ] +}", result); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 48b759fc..557ffd89 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -227,7 +227,7 @@ namespace Newtonsoft.Json.Linq foreach (object c in enumerable) { AddInternal(isLast, multiPrevious, c); - multiPrevious = (previous != null) ? previous._next : Last; + multiPrevious = (multiPrevious != null) ? multiPrevious._next : Last; } } else