Home > ASP.NET > Sort json Array with LINQ and JSON.NET

Sort json Array with LINQ and JSON.NET

string jsonStr = dal.GetUsersByType(userId, userType);
JObject json = JObject.Parse(jsonStr);

IEnumerable<JToken> users = from p in json[“Users”]
orderby p[“Firstname”]
select p;

return JsonConvert.SerializeObject(users);

I got it. Some complex join queries may not allow you to sort (order by) a particular column.

So my way is , first get the json array, convert to Json object using JSON.NET.

Then sort it using LINQ and serialize back to json string.

Sample code is shown above.

Happy Coding 🙂

Advertisements
Categories: ASP.NET Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: