简体   繁体   中英

Parse LINQ answer in a JSON

I'm working in a ASP.NET webservice solution. I connect to a Database using Entity framework connection. I want to return a JSON like this:

[{"DIM_FECHA":[201502,201503,201504]}{"FCT_TOTALFACT":[1234567,1234555,1234444]}]

Now, I'm getting in a separate "rows":

[{"DIM_FECHA":201502,"FCT_TOTALFACT":1234567},{"DIM_FECHA":201503,"FCT_TOTALFACT":1784578},{"DIM_FECHA":201504,"FCT_TOTALFACT":1784561},{"DIM_FECHA":201505,"FCT_TOTALFACT":1254567},{"DIM_FECHA":201506,"FCT_TOTALFACT":1879016},{"DIM_FECHA":201507,"FCT_TOTALFACT":1123456}]

My code is this:

using (Entities database = new Entities())
{
    var Agr = from P in database.AGR_FACTURACION
                     where P.DIM_FECHA > 201501
                     select new { P.DIM_FECHA,
              P.FCT_TOTALFACT};
    JavaScriptSerializer js = new JavaScriptSerializer();
    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.Write(js.Serialize(Agr));
}

Only I want to know if it is possible getting it without doing a loop over all results.

Thanks in advance and sorry about my english.

var Agr = (from P in database.AGR_FACTURACION
          where P.DIM_FECHA > 201501
          select new { P.DIM_FECHA, P.FCT_TOTALFACT}
          ).ToArray();

var result = new {DIM_FECHA = Agr.Select(o=>o.DIM_FECHA), FCT_TOTALFACT = Agr.Select(o=>o.FCT_TOTALFACT)};
...
Context.Response.Write(js.Serialize(result));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM