Not able to return JsonResult

前端 未结 2 1804
暖寄归人
暖寄归人 2021-02-09 20:05

The following query is working successfully.

var tabs = (
                from r in db.TabMasters
                orderby r.colID
                select new { r.         


        
2条回答
  •  礼貌的吻别
    2021-02-09 20:16

    I suspect that it's as simple as pushing the last part into an in-process query using AsEnumerable():

    var jsonData = new
    {
        total = (int)Math.Ceiling((float)totalRecords / (float)rows),
        page = page,
        records = totalRecords,
        rows = (from r in tabs.AsEnumerable()
                select new { id = r.colID,
                             cell = new[] { r.FirstName, r.LastName } }
               ).ToArray()
    };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
    

    You may want to pull that query out of the anonymous type initializer, for clarity:

    var rows = tabs.AsEnumerable()
                   .Select(r => new { id = r.colID,
                                      cell = new[] { r.FirstName, r.LastName })
                   .ToArray();
    
    var jsonData = new { 
        total = (int)Math.Ceiling((float)totalRecords / (float)rows),
        page,
        records = totalRecords,
        rows
    };
    

提交回复
热议问题