不能够返回JsonResult [英] Not able to return JsonResult

查看:129
本文介绍了不能够返回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像

Now I want to return JsonResult as like

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]'来代替。

我应该怎么做才能得到预期的结果?

What should I do to get expected result?

推荐答案

我怀疑这是由于使用推最后一部分成一个进程查询 AsEnumerable():

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
};

这篇关于不能够返回JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆