2011-03-18 91 views
1

我正在尝试构建一个jqgrid。服务器代码返回错误?:错误:LINQ to Entities不能识别该方法

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

回答

0

尝试使用此代码

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参数。

相关问题