Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ to实体无法识别该方法。。。方法,而此方法无法转换为存储表达式。_C#_Asp.net - Fatal编程技术网

C# LINQ to实体无法识别该方法。。。方法,而此方法无法转换为存储表达式。

C# LINQ to实体无法识别该方法。。。方法,而此方法无法转换为存储表达式。,c#,asp.net,C#,Asp.net,MVC 5,C#- 我正在根据数据库中的不同数据集创建视图模型。我有以下代码来创建视图模型 public ActionResult Register_step6() { CoreGeneral cg = new CoreGeneral(); var model = (from p in db.WorkPointRoles select new RegisterEmployee_Step6Model()

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实体无法识别方法“System.String” GetRoleDescription(Int32)'方法,并且不能使用此方法 翻译成商店表达式

我理解该消息与无法识别我的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);

}

GetRoleDescription
有什么作用?谢谢,效果很好!这是一个我还不太喜欢的地方!您可以操作第一个select查询,只使用
db.WorkFopintRoles.AsEnumerable().select(…)
select
查询仅保留在LINQ查询的控件中。