LINQ to Entities不能识别'System.String方法

本文关键字:System String 方法 to Entities 不能 识别 LINQ | 更新日期: 2023-09-27 17:55:06

我在taskData记录列表。

tasksData = MyTaskManager.GetAllTask().Select(x => new MyTaskContract
{                
    TaskMileStone=GetTaskMileStone(x.TaskMileStone
}
Int32  totalRecords = tasksData.Count(); // Here I got exception

/*  Some error has occurred the description of which is: LINQ to Entities
     does not recognize the method 'System.String 
     GetTaskMileStone(System.Nullable`1[System.Int32])' 
     method, and this method cannot be translated into a store expression.  */
taskmilestone返回0,1,2,3
private String GetTaskMileStone(Int32? isMileStone)
{
        String milestoneName = String.Empty;
        switch (isMileStone)
        {
            case AppConsts.ONE:
                milestoneName = "PayPoint";
                break;
            case AppConsts.TWO:
                milestoneName = "Cost Point";
                break;
            case AppConsts.THREE:
                milestoneName = "Milestone";
                break;
            case AppConsts.NONE:
                milestoneName = "NO";
                break;
        }
        return milestoneName;
}

LINQ to Entities不能识别'System.String方法

是的,你不能在实体框架中调用自定义方法。

感谢上帝,这是一个解决办法。

确保你离开实体框架。通过插入一个AsEnumrable调用来实现这一点——这样查询的下一个"阶段"就会作为对对象的LINQ执行。

这条线

:

tasksData = MyTaskManager.GetAllTask()。Select(x => new MyTaskContract

变成:

tasksData = MyTaskManager.GetAllTask(). asenumerable()。Select(x => new MyTaskContract

那么最后的选择与投影将发生在对象上使用Linq到对象,在那里你可以调用一个方法。

相关文章: