I am trying to display multiple username with their image So, I have a Json action method like this:
public JsonResult GetUsers()
{
var ret = (from user
GetFileData
couldn't be translated to T-SQL, Linq to Entities couldn't recognize it. You can modify the code as below (Move the GetFileData
out of expression):
var pic = GetFileData(user.Id);
public JsonResult GetUsers()
{
var ret = (from user in db.Users
orderby user.UserName
select new
{
UserName = user.UserName,
Pic = pic,
}).AsEnumerable();
return Json(ret, JsonRequestBehavior.AllowGet);
}
But because user does not exist outside the query you should use .ToList()
to defer the use of your function. With .ToList()
after data is loaded, any further operation (such as select
) is performed using Linq to Objects, on the data already in memory. So your query should be like this:
public JsonResult GetUsers()
{
var ret = (from user in db.Users.ToList()
orderby user.UserName
select new
{
UserName = user.UserName,
Pic = GetFileData(user.Id),
}).AsEnumerable();
return Json(ret, JsonRequestBehavior.AllowGet);
}