MVC 5,C#-
我正在基于数据库中的不同数据集创建视图模型。我有以下代码来创建视图模型。
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无法识别方法'System.String
GetRoleDescription(Int32)'方法,并且该方法不能为
转换为商店表达式。
我了解到该消息与实体无法识别我的GetRoleDescription代码有关,因为它放置在select语句中-(我可能误解了我的理解)。无论如何,我该如何解决问题,在哪里可以找到把GetRoleDescription,以便它在返回的每个记录上运行?
我希望这对某人有意义!对于措辞不佳的问题表示歉意。提前致谢!
最佳答案
您应该对内存中的RegisterEmployee_Step6Model
而不是SQL Server端执行投影。
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);
}