错误:LINQ to Entities 无法识别该方法

发布于 2024-10-22 18:46:35 字数 1389 浏览 0 评论 0原文

我正在尝试构建一个jqgrid。服务器代码返回错误?:

LINQ to Entities 无法识别“System.String ToString()”方法,并且此方法无法转换为存储表达式。

代码:

public ActionResult GridData(string sidx, string sord, int page, int rows)
{

    ProductsEntities4 db = new ProductsEntities4();
    var jsondata = new {
            total = 1,
            page = 1,
            records = 2,
            rows = (from pr in db.Products
                    select new {
                        i = pr.Id,
                        cell = new string[] { pr.Id.ToString(), pr.ProductName }
                    }).ToArray()
            };

    //return jsondata;
    return Json(jsondata, JsonRequestBehavior.AllowGet);
}

哪里

<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/Home/GridData/',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Id', 'ProductName'],
        colModel: [
      { name: 'Id', index: 'Id', width: 40, align: 'left' },
      { name: 'ProductName', index: 'ProductName', width: 40, align: 'left' }],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: '/scripts/themes/coffee/images',
        caption: 'My first grid'
    });
}); 

i am trying to build a jqgrid. the server code is returning an error?:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

code:

public ActionResult GridData(string sidx, string sord, int page, int rows)
{

    ProductsEntities4 db = new ProductsEntities4();
    var jsondata = new {
            total = 1,
            page = 1,
            records = 2,
            rows = (from pr in db.Products
                    select new {
                        i = pr.Id,
                        cell = new string[] { pr.Id.ToString(), pr.ProductName }
                    }).ToArray()
            };

    //return jsondata;
    return Json(jsondata, JsonRequestBehavior.AllowGet);
}

where

<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/Home/GridData/',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Id', 'ProductName'],
        colModel: [
      { name: 'Id', index: 'Id', width: 40, align: 'left' },
      { name: 'ProductName', index: 'ProductName', width: 40, align: 'left' }],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: '/scripts/themes/coffee/images',
        caption: 'My first grid'
    });
}); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-10-29 18:46:35

尝试使用此代码

public ActionResult GridData(string sidx, string sord, int page, int rows)
{

    ProductsEntities4 db = new ProductsEntities4();
    var products = db.Products
                   .OrderBy ("it." + sidx + " " + sord).Skip ((page - 1)* rows)
                   .Take (rows);
    var totalRecords = products.Count ();

    // to be able to use ToString() below which is NOT exist in the LINQ to Entity
    // we get the properties which we need and convert results to List
    var productData = (from product in products
                       select new { product.Id, product.ProductName }).ToList ();

    var jsondata = new {
            total = (totalRecords + rows - 1) / rows,
            page = 1,
            records = totalRecords,
            rows = (from pr in productData
                    select new {
                        id = pr.Id,
                        cell = new string[] { pr.Id.ToString(), pr.ProductName }
                    }).ToList()
            };

    //return jsondata;
    return Json(jsondata, JsonRequestBehavior.AllowGet);
}

并从 jqGrid 中删除已弃用的 imgpath 参数。

Try with this code

public ActionResult GridData(string sidx, string sord, int page, int rows)
{

    ProductsEntities4 db = new ProductsEntities4();
    var products = db.Products
                   .OrderBy ("it." + sidx + " " + sord).Skip ((page - 1)* rows)
                   .Take (rows);
    var totalRecords = products.Count ();

    // to be able to use ToString() below which is NOT exist in the LINQ to Entity
    // we get the properties which we need and convert results to List
    var productData = (from product in products
                       select new { product.Id, product.ProductName }).ToList ();

    var jsondata = new {
            total = (totalRecords + rows - 1) / rows,
            page = 1,
            records = totalRecords,
            rows = (from pr in productData
                    select new {
                        id = pr.Id,
                        cell = new string[] { pr.Id.ToString(), pr.ProductName }
                    }).ToList()
            };

    //return jsondata;
    return Json(jsondata, JsonRequestBehavior.AllowGet);
}

and remove deprecated imgpath parameter from the jqGrid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文