LINQ to Entities does not recognize the method 'System.Web.Mvc.FileResult'

后端 未结 1 1690
盖世英雄少女心
盖世英雄少女心 2020-11-28 15:31

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         


        
相关标签:
1条回答
  • 2020-11-28 16:15

    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);
    }
    
    0 讨论(0)
提交回复
热议问题