Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any workaround for lack of support for PadLeft in EF5.x?

I'm working on an app in MVC4 and Entity Framework 5 and recently came across this exception when executing my query.

{"LINQ to Entities does not recognize the method 'System.String PadLeft(Int32, Char)' method, and this method cannot be translated into a store expression."}

When I have run across similar errors in the past, I've just made a variable outside the query and then used the variable in the LINQ statement. Unfortunately, in this case I'm manipulating the row results so I'm not sure how to go about that or if that is the best method. Any help would be appreciated. My query is below:

            IQueryable<System.String> LEAPrograms = db.Datamart_Draft
            .Where(where => where.snapshot_id == snapshot_id
                && !String.IsNullOrEmpty(where.entity_program))
            .Select(sel => (sel.entity_program.PadLeft(PROGRAMLENGTH, '0'))).Distinct();
like image 584
Elsimer Avatar asked Feb 28 '13 14:02

Elsimer


1 Answers

It's not too elegant but it does the job:

...
.Select(sel => SqlFunctions.Replicate
                   ("0", PROGRAMLENGTH - sel.entity_program.Length)
             + sel.entity_program)
like image 68
Gert Arnold Avatar answered Oct 28 '22 04:10

Gert Arnold