Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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# 使用Linq对JSON对象进行排序_C#_Linq To Objects_Json.net - Fatal编程技术网

C# 使用Linq对JSON对象进行排序

C# 使用Linq对JSON对象进行排序,c#,linq-to-objects,json.net,C#,Linq To Objects,Json.net,我从一个谷歌搜索设备建议服务那里得到了一个JSON格式的回复,格式如下 string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}"; 我想按名称字母顺序对结果列表进行排序,并将名称改为句子大小写。 我可以在jquery中执行此操作,但出于性能原因,我更愿意

我从一个谷歌搜索设备建议服务那里得到了一个JSON格式的回复,格式如下

string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}";
我想按名称字母顺序对结果列表进行排序,并将名称改为句子大小写。 我可以在jquery中执行此操作,但出于性能原因,我更愿意在服务器端执行此操作

我可以对结果进行排序,但这会返回一个
IEnumarable
,但我似乎无法在正在序列化的对象中对结果进行排序

 string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}";

JObject json = JObject.Parse(jsonString);

        var gsaSuggestions = JsonConvert.DeserializeObject<GSASuggestion>(jsonString);

        var orded = gsaSuggestions.ResultList.OrderBy<Result, string>(r => r.Name);

        string output = JsonConvert.SerializeObject(gsaSuggestions);
    }

    [JsonObject(MemberSerialization.OptOut)]
    public class GSASuggestion
    {
        [JsonProperty(PropertyName = "query")]
        public string Query {get; set;}
        [JsonProperty(PropertyName = "results")]
        public List<Result> ResultList {get; set;}
    }

    public class Result
    {
        [JsonProperty(PropertyName = "name")]
        public string Name {get; set;}
        [JsonProperty(PropertyName = "type")]
        public string Type {get; set;}
    }

实际上,您没有使用OrderBy的返回值。尝试:

gsaSuggestions.ResultList =
    gsaSuggestions.ResultList.OrderBy<Result, string>(r => r.Name).ToList();
gsaSuggestions.ResultList =
    gsaSuggestions.ResultList.OrderBy<Result, string>(r => r.Name).ToList();
gsaSuggestions.ResultList.Sort((x, y) => x.Name.CompareTo(y.Name));