首页 > 解决方案 > C# LINQ to Entities 无法识别方法 'System.String 方法,并且此方法无法转换为存储表达式

问题描述

这只是我的情况的一个例子

 var query =
    Employee.Join(department,
                emp => emp.depId,
                dep=> dep.Id,
                (emp, dep) =>
                    new EmployeeModel{ Name = emp.Name, Total = GetTotal(emp)});

   

    public string GetTotal(emp)
{
    //dynamically decide which column to pass to the stored procedure, that is 
    //why I am passing the whole object
    total = sp(emp.column1, emp.column2);//here I pass the parameters to SP    
    return total;
}

我在这里遇到了一个异常,我不知道如何解决它。任何帮助将不胜感激。谢谢

标签: c#.netlinq

解决方案


这当然会发生。Linq to entity 不知道是什么GetTotal(),并且考虑到它将如何为返回的每一行调用存储过程,我建议您将连接本身移动到存储过程中,暂时忘记 LINQ。


推荐阅读