错误LINQ到实体无​​法识别方法'System.String的ToString()“方法,而这种方法不能被翻译成店前pression [英] Error LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

查看:181
本文介绍了错误LINQ到实体无​​法识别方法'System.String的ToString()“方法,而这种方法不能被翻译成店前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟MVC4,Entityframewor和工作的jqGrid,当我从读取数据库中的数据,我stucked本error.Many的你说的填充id字段来anothe变种,但IA没有得到确切的地方来写,我的在数据库ID字段是整数。所以请不要帮我。
    u.Id是我从EF访问ID字段中,它显示出此错误。什么是另一种方法,并在那里把新的code。
    我的控制器看起来像

I am working with MVC4 , Entityframewor and Jqgrid, when am fetching data from Database , i stucked with this error.Many of you said populate the id field to anothe var , but i a not getting where exactly to write it, and my Id field in Database is of Integer. so please do help me. u.Id is a Id field which i am accessing from EF, It showing this error. what is the alternate way, and where to put the new code. My Controller looks like

public JsonResult GetUserDetails(string sidx="Id", string sord="asc", int page=1, int rows=5)
{

    int pageIndex = Convert.ToInt32(page) - 1;
    int pageSize = rows;
    int totalRecords = db.Users.Count();
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
    var userdata = db.Users.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);

    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,

        rows = (from u in userdata
                select new
                {
                    i = u.Id,
                    cell = new string[]{**u.Id.ToString()**, u.Name,u.Designation,u.City}
                    //cell = new string[] { "", "", "", "" }
                }).ToArray()
    };
    return Json(jsonData);
}

我从过去每天的工作这一点,而不是从该得到缓解。

I am Working this from past a day, and not getting relief from this .

推荐答案

toString()方法不能转换为SQL查询。所以,你有几种选择:

Method ToString() cannot be translated to SQL query. So you have several options:


  1. 您可以从数据库中获得完整的用户数据实体,它在.NET code映射到字符串数组:

  1. You can get full entity userdata from db and map it to string array in .net code:

var userdata = db.Users.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize).AsEnumerable();

var jsonData = new
{

    total = totalPages,
    page,
    records = totalRecords,

    rows = (
        from u in userdata
        select new
        {
            i = u.Id,
            cell = new string[]{**u.Id.ToString()**, u.Name,u.Designation,u.City}
            //cell = new string[] { "", "", "", "" }
        }).ToArray()
};


2.You可以使用两个选择(),首先从数据库获取数据,第二个它映射到字符串数组:

2.You can use two Select(), first to get data from db, second to map it to your string array:

var userdata = db.Users.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize).Select(u=>new{u.Id, u.Name, u.Designation, u.City});

var jsonData = new
{

    total = totalPages,
    page,
    records = totalRecords,

    rows = (
        from u in userdata.AsEnumerable()
        select new
        {
            i = u.Id,
            cell = new string[]{u.Id.ToString(), u.Name,u.Designation,u.City}
        }).ToArray()
};

这篇关于错误LINQ到实体无​​法识别方法'System.String的ToString()“方法,而这种方法不能被翻译成店前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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