404 Not found
404 Not found

Reputation: 201

LINQ to Entities does not recognize the method 'System.String

I have list of record in 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.  */

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

Upvotes: 1

Views: 1762

Answers (1)

TomTom
TomTom

Reputation: 62093

Yes, you can not call custom methods in entity framework.

Thanks heaven thee is a workaround.

MAke sure you leave entity framework. Do this by inserting an AsEnumrable call - so the next "Phase" of the query is then executed as LINQ to objets.

This line:

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

turns into:

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

then the last select with the projection will happen on the objects using Linq To Objects and there you can call a method.

Upvotes: 2

Related Questions