asp.net – 无法返回JsonResult

以下查询已成功运行.
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
};

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...