Convert IEnumerable<dynamic> to JsonArray

Posted by Burt on Stack Overflow See other posts from Stack Overflow or by Burt
Published on 2012-04-06T16:48:22Z Indexed on 2012/04/06 17:28 UTC
Read the original article Hit count: 437

Filed under:
|

I am selecting an IEnumerable<dynamic> from the database using Rob Conery's Massive framework. The structure comes back in a flat format Poco C#. I need to transform the data and output it to a Json array (format show at bottom).

I thought I could do the transform using linq (my unsuccessful effort is shown below):

using System.Collections.Generic;
using System.Json;
using System.Linq;
using System.ServiceModel.Web;

....
    IEnumerable<dynamic> list = _repository.All("", "", 0).ToList();

    JsonArray returnValue = from item in list
                            select new JsonObject()
                                       {
                                               Name = item.Test,
                                               Data = new dyamic(){...}...
                                       };

Here is the Json I am trying to generate:

[
    {
        "id": "1",
        "title": "Data Title",
        "data": [
            {
                "column1 name": "the value",
                "column2 name": "the value",
                "column3 name": "",
                "column4 name": "the value"
            }
        ]
    },
    {
        "id": "2",
        "title": "Data Title",
        "data": [
            {
                "column1 name": "the value",
                "column2 name": "the value",
                "column3 name": "the value",
                "column4 name": "the value"
            }
        ]
    }
]

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ