LINQ到实体无​​法识别方法“System.Web.Mvc.FileResult” [英] LINQ to Entities does not recognize the method 'System.Web.Mvc.FileResult'

查看:136
本文介绍了LINQ到实体无​​法识别方法“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 是与文件外键和 ApplicationUser 类。

现在,当我运行查询,我应该得到的用户名与PIC但我正在逐渐 System.NotSupportedException .The完整的错误信息是:

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实体无法识别方法
  System.Web.Mvc.FileResult GetFileData(Int32)已的方法,这种方法
  不能被翻译成店前pression。

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.

如何获得它resolved.I看到有关它的许多问题,计算器,但没有这个问题被有关FileResult.The code为视图页面是这里的 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到实体无​​法识别它。您可以修改code如下(移动 GetFileData 出前pression的):

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()加载数据后,使用进行任何进一步的操作(如选择)的 LINQ到对象的,对已在内存中的数据。因此,您的查询应该是这样的:

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

这篇关于LINQ到实体无​​法识别方法“System.Web.Mvc.FileResult”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆