<p>I'm stuck trying to create a dynamic linq extension method that returns a string in JSON format - I'm using System.Linq.Dynamic and Newtonsoft.Json and I can't get the Linq.Dynamic to parse the "cell=new object[]" part. Perhaps too complex? Any ideas? :  </p>

<p><strong>My Main method:</strong></p>

<pre><code>static void Main(string[] args)
{
    NorthwindDataContext db = new NorthwindDataContext();
    var query = db.Customers;
    string json = JSonify&lt;Customer&gt;
                    .GetJsonTable(
                        query, 
                        2, 
                        10, 
                        "CustomerID"
                        , 
                        new string[] 
                            { 
                                "CustomerID", 
                                "CompanyName", 
                                "City", 
                                "Country", 
                                "Orders.Count"
                            });
    Console.WriteLine(json);
}
</code></pre>

<p><strong>JSonify class</strong></p>

<pre><code>public static class JSonify&lt;T&gt;
{
    public static string GetJsonTable(
        this IQueryable&lt;T&gt; query, 
        int pageNumber, 
        int pageSize, 
        string IDColumnName, 
        string[] columnNames)
    {
        string selectItems =
            String.Format(@"
                        new
                        {
                            {{0}} as ID,
                            cell = new object[]{{{1}}}
                        }", 
                          IDColumnName, 
                          String.Join(",", columnNames));

        var items = new
        {
            page = pageNumber,
            total = query.Count(),
            rows =
                query
                    .Select(selectItems)
                    .Skip(pageNumber * pageSize)
                    .Take(pageSize)
        };

        return JavaScriptConvert.SerializeObject(items);
        // Should produce this result:
        // {
        //    "page":2,
        //    "total":91,
        //    "rows":
        //        [
        //        {"ID":"FAMIA","cell":["FAMIA","Familia Arquibaldo","Sao Paulo","Brazil",7]},
        //        {"ID":"FISSA","cell":["FISSA","FISSA Fabrica Inter. Salchichas S.A.","Madrid","Spain",0]},
        //        {"ID":"FOLIG","cell":["FOLIG","Folies gourmandes","Lille","France",5]},
        //        {"ID":"FOLKO","cell":["FOLKO","Folk och fä HB","Bräcke","Sweden",19]},
        //        {"ID":"FRANK","cell":["FRANK","Frankenversand","München","Germany",15]},
        //        {"ID":"FRANR","cell":["FRANR","France restauration","Nantes","France",3]},
        //        {"ID":"FRANS","cell":["FRANS","Franchi S.p.A.","Torino","Italy",6]},
        //        {"ID":"FURIB","cell":["FURIB","Furia Bacalhau e Frutos do Mar","Lisboa","Portugal",8]},
        //        {"ID":"GALED","cell":["GALED","Galería del gastrónomo","Barcelona","Spain",5]},
        //        {"ID":"GODOS","cell":["GODOS","Godos Cocina Típica","Sevilla","Spain",10]}
        //        ]
        // }

    }

}
</code></pre>

[{'answer_id': 50592, 'author': 'Scott Nichols', 'author_id': 4299, 'author_profile': 'https://Stackoverflow.com/users/4299', 'pm_score': 0, 'selected': False, 'text': '<pre><code>static void Main(string[] args)\n{\n    NorthwindDataContext db = new NorthwindDataContext();\n    var query = db.Customers;\n    string json = query.GetJsonTable&lt;Customer&gt;(2, 10, "CustomerID", new string[] {"CustomerID", "CompanyName", "City", "Country", "Orders.Count" });\n }  \n\npublic static class JSonify\n{\n    public static string GetJsonTable&lt;T&gt;(\n        this IQueryable&lt;T&gt; query, int pageNumber, int pageSize, string IDColumnName, string[] columnNames)\n    {\n        string select = string.Format("new ({0} as ID, new ({1}) as cell)", IDColumnName, string.Join(",",     columnNames));\n        var items = new\n        {\n            page = pageNumber,\n            total = query.Count(),\n            rows = query.Select(select).Skip((pageNumber - 1) * pageSize).Take(pageSize)\n        };\n        return JavaScriptConvert.SerializeObject(items);\n    }\n}  \n</code></pre>\n'}, {'answer_id': 52151, 'author': 'Robert Dean', 'author_id': 3396, 'author_profile': 'https://Stackoverflow.com/users/3396', 'pm_score': 1, 'selected': False, 'text': '<p>Thanks for the quick response.\nHowever, note the required output does not have property names in the "cell" array ( that\'s why I was using object[]):</p>\n\n<p>"cell":["FAMIA","Familia Arquibaldo",... \nvs. \n"cell":{"CustomerID":"FAMIA","CompanyName","Familia Arquibaldo",...</p>\n\n<p>The result is meant to be used with a JQuery grid called "flexify" which requires the output in this format. </p>\n'}, {'answer_id': 55511, 'author': 'Scott Nichols', 'author_id': 4299, 'author_profile': 'https://Stackoverflow.com/users/4299', 'pm_score': 3, 'selected': True, 'text': '<p>This is really ugly and there may be some issues with the string replacement, but it produces the expected results:</p>\n\n<pre><code>public static class JSonify\n{\n    public static string GetJsonTable&lt;T&gt;(\n        this IQueryable&lt;T&gt; query, int pageNumber, int pageSize, string IDColumnName, string[] columnNames)\n    {\n        string select = string.Format("new ({0} as ID, \\"CELLSTART\\" as CELLSTART, {1}, \\"CELLEND\\" as CELLEND)", IDColumnName, string.Join(",", columnNames));\n        var items = new\n        {\n            page = pageNumber,\n            total = query.Count(),\n            rows = query.Select(select).Skip((pageNumber - 1) * pageSize).Take(pageSize)\n        };\n        string json = JavaScriptConvert.SerializeObject(items);\n        json = json.Replace("\\"CELLSTART\\":\\"CELLSTART\\",", "\\"cell\\":[");\n        json = json.Replace(",\\"CELLEND\\":\\"CELLEND\\"", "]");\n        foreach (string column in columnNames)\n        {\n            json = json.Replace("\\"" + column + "\\":", "");\n        }\n        return json;\n    }\n}  \n</code></pre>\n'}]
['https://Stackoverflow.com/questions/50251', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3396/']