以下查询已成功运行.
var tabs = ( from r in db.TabMasters orderby r.colID select new { r.colID,r.FirstName,r.LastName }) .Skip(rows * (page - 1)).Take(rows);
现在我想要返回JsonResult
var jsonData = new { total = (int)Math.Ceiling((float)totalRecords / (float)rows),page = page,records = totalRecords,rows = (from r in tabs select new { id = r.colID,cell = new string[] { r.FirstName,r.LastName } }).ToArray() }; return Json(jsonData,JsonRequestBehavior.AllowGet);
但它会给我一个错误,如:
无法在查询结果中初始化数组类型’System.String []’.请考虑使用’System.Collections.Generic.List`1 [System.String]’.
我该怎么做才能得到预期的结果?
解决方法
我怀疑它就像使用AsEnumerable()将最后一部分推入进程内查询一样简单:
var jsonData = new { total = (int)Math.Ceiling((float)totalRecords / (float)rows),rows = (from r in tabs.AsEnumerable() select new { id = r.colID,cell = new[] { r.FirstName,r.LastName } } ).ToArray() }; return Json(jsonData,JsonRequestBehavior.AllowGet);
var rows = tabs.AsEnumerable() .Select(r => new { id = r.colID,r.LastName }) .ToArray(); var jsonData = new { total = (int)Math.Ceiling((float)totalRecords / (float)rows),page,rows };