我正在使用ASP.NET Web服务解决方案。 我使用Entity框架连接连接到数据库。 我想要返回一个像这样的JSON:
[{"DIM_FECHA":[201502,201503,201504]}{"FCT_TOTALFACT":[1234567,1234555,1234444]}]
现在,我进入了一个单独的"行":
[{"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}]
我的代码是:
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));
}
只有我想知道是否可以在不对所有结果进行循环的情况下获取它。
提前致谢并对我的英语感到抱歉。
答案 0 :(得分:1)
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));