GetFileData
cannot be translated into T-SQL, Linq to Entities could not recognize it. You can change the code as shown below (Move GetFileData
outside the 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 since the user does not exist outside the request, you must use .ToList()
to defer the use of your function. After .ToList()
after loading the data, any further operation (for example, select
) is performed using Linq to Objects, according to the data already in memory. Therefore, your request should look 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); }
source share