0

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 .

Abbas
  • 14,186
  • 6
  • 41
  • 72
Nandish Hosmane
  • 107
  • 2
  • 18

1 Answers1

1

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

  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 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()
};
Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
  • Thank you Kirill, it worked. and i have another issue if you don't mind i will ask, its about Jqgrid, i am returning the above data to my jqgrid, which is in my view. but his data is returning as series of values, but JQgrid is not at all showing up. if you wish i can send you my project its a sample project. please check out. – Nandish Hosmane Feb 17 '14 at 09:55
  • I didnot work with jqgrid, but you can create new question about new problem and someone will propose you solution. – Kirill Bestemyanov Feb 17 '14 at 10:05