加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 无法返回JsonResult

发布时间:2020-12-15 20:54:02 所属栏目:asp.Net 来源:网络整理
导读:以下查询已成功运行. 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)tot
以下查询已成功运行.
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
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读