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/