LINQ to Entities无法识别方法' System.String

时间:2014-03-31 05:53:37

标签: c# entity-framework

我在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.  */

x.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;
}

1 个答案:

答案 0 :(得分:2)

是的,您无法在实体框架中调用自定义方法。

谢谢天堂,你是一个解决方法。

确保您离开实体框架。通过插入一个AsEnumrable调用来实现这一点 - 所以下一个"阶段"然后将查询执行为LINQ to objets。

这一行:

  

tasksData = MyTaskManager.GetAllTask​​()。选择(x => new MyTaskContract

变成:

  

tasksData = MyTaskManager.GetAllTask​​()。AsEnumerable()。选择(x => new MyTaskContract

然后使用Linq To Objects在对象上进行最后一次投影选择,你可以在那里调用一个方法。