c# - LINQ to Entities 无法识别方法...方法,并且该方法无法转换为存储表达式。

标签 c# asp.net

MVC 5、C# - 我正在根据数据库中的不同数据集创建 View 模型。我有以下代码来创建 View 模型。

    public ActionResult Register_step6()
    {
        CoreGeneral cg = new CoreGeneral();

        var model = (from p in db.WorkPointRoles
                     select new RegisterEmployee_Step6Model()

                     {
                         Role = p.Role,
                         Selected = false,
                         RoleDescription = cg.GetRoleDescription(p.Id)
                     });

        return View(model);

    }

我收到以下错误消息:

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

我知道该消息与实体无法识别我的 GetRoleDescription 代码有关,因为它被放置在 select 语句中 - (我可能错误地表达了我的理解)..无论如何,我该如何解决这个问题,我可以将 GetRoleDescription 放在哪里,以便它在返回的每条记录上运行?

我希望这对某人有意义!对于措辞不当的问题表示歉意。提前致谢!

最佳答案

您应该在内存中而不是 SQL Server 端执行投影到 RegisterEmployee_Step6Model

public ActionResult Register_step6()
{
    CoreGeneral cg = new CoreGeneral();

    var model = (from p in db.WorkPointRoles
                 select p).AsEnumerable()
               .Select(r => new RegisterEmployee_Step6Model()
                 {
                     Role = r.Role,
                     Selected = false,
                     RoleDescription = cg.GetRoleDescription(r.Id)
                 });

    return View(model);

}

关于c# - LINQ to Entities 无法识别方法...方法,并且该方法无法转换为存储表达式。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38813424/

相关文章:

c# - Convert.toInt32(boolean) 是 c# 中执行此操作的唯一方法吗?

c# - 安全异常 ASP.NET/MySQL

c# - 如何使用 C# 在字符串的特定索引处插入字符?

c# - 使用 OpenFileDialog 作为目录,而不是 FolderBrowserDialog

c# - 如何在不更改行的情况下查看 DataGridView 更改

c# - 无效的列名 C#

asp.net - asp.net 的 GWT 很好的替代品

c# - 如何缩短 Asp.net Web 应用程序调试时间?

c# - 在同一时间发送短信

c# - 半临时报告的通用接口(interface)