LINQ to Entities 无法识别方法“System.Web.Mvc.FileResult"

问题描述:

我试图用他们的图像显示多个用户名所以,我有一个像这样的 Json 操作方法:

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 in db.Users
                orderby user.UserName
                select new
                {
                    UserName = user.UserName,
                    Pic = GetFileData(user.Id),
                }).AsEnumerable();
     return Json(ret, JsonRequestBehavior.AllowGet);
}

这是我的 GetFileData 获取用户图像的方法:

And this is my GetFileData method to get user's image:

public FileResult GetFileData(int Id)
{
    var avatarImage = db.Files.SingleOrDefault(s => s.ApplicationUserId == Id);
    return File(avatarImage.Content, avatarImage.ContentType);
}

这里,ApplicationUserId 是与 File 和 ApplicationUser 类相关的外键.

Here, ApplicationUserId is the foreign key relating File and ApplicationUser class.

现在,当我运行查询时,我应该使用他们的图片获取用户名但我得到 System.NotSupportedException.完整的错误消息是:

Now, when I run the query, I should get username with their pic But I am getting System.NotSupportedException.The complete error message is:

LINQ to Entities 无法识别该方法'System.Web.Mvc.FileResult GetFileData(Int32)'方法,以及这个方法不能翻译成商店表达.

LINQ to Entities does not recognize the method 'System.Web.Mvc.FileResult GetFileData(Int32)' method, and this method cannot be translated into a store expression.

如何解决它.我见过很多与它相关的 stackoverflow 问题,但没有一个问题与 FileResult 相关.查看页面的代码在这里 http://pastebin.com/AyrpGgEm

How to get it resolved.I have seen many stackoverflow question relating it but none of the question was relating to FileResult.The code for view page is here http://pastebin.com/AyrpGgEm

GetFileData 无法转换为 T-SQL,Linq to Entities 无法识别.您可以修改如下代码(将 GetFileData 移出表达式):

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);
}

但是因为用户不存在于查询之外,所以你应该使用 .ToList() 来推迟你的函数的使用.使用 .ToList() 数据加载后,任何进一步的操作(例如 select)都使用 Linq to Objects 对已经存在的数据执行在记忆中.所以你的查询应该是这样的:

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);
}