This question already has an answer here:
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 .
Method ToString() cannot be translated to SQL query. So you have several options:
You can get full entity userdata from db and map it to string array in .net code:
2.You can use two
Select()
, first to get data from db, second to map it to your string array: