Any workaround due to lack of PadLeft support in EF5.x?

I am working on an application in MVC4 and Entity Framework 5 and have recently encountered this exception when executing my request.

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

When I came across similar errors in the past, I just made a variable outside the query and then used the variable in the LINQ statement. Unfortunately, in this case I am manipulating the results of the string, so I'm not sure how to do this, or if this is the best method. Any help would be greatly appreciated. My request 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();
+5
source share
3 answers

, . .Distinct() , IEnumerable. , , , PadLeft LINQ to Entities, .

            IEnumerable<String> LEAPrograms = db.Datamart_Draft.Where(wh => wh.snapshot_id == snapshot_id && !String.IsNullOrEmpty(wh.entity_program)).Select(se => se.entity_program).Distinct();
        // create and populate a List (because LINQ to Entities doesn't support PadLeft)
        List<String> PaddedPrograms = new List<String>();
        foreach (var row in LEAPrograms)
        {
            PaddedPrograms.Add(row.PadLeft(4, '0'));
        }
        LEAPrograms = PaddedPrograms.Distinct();
-4

, :

...
.Select(sel => SqlFunctions.Replicate
                   ("0", PROGRAMLENGTH - sel.entity_program.Length)
             + sel.entity_program)
+12

, . , :

List<string> samplesWithoutMeasures = new List<string>();
samplesWithoutMeasures = (from mm in DB.MEDICIONESMUESTRA
   join mu in DB.MUESTRAS on mm.IDMUESTRA equals mu.IDMUESTRA
   where    (mu.IDESTADOMUESTRA >= 7 && mu.IDESTADOMUESTRA <= 8) && (mu.ESDUPLICADODE == null) &&
            (mm.IDESTADOMEDICIONMUESTRA == 1 &&  mm.IDPARAMETRO == Parameter.IDPARAMETRO) &&
            (mu.FECHARADICACION >= StartDate && mu.FECHARADICACION <= EndDate)
  select    SqlFunctions.Replicate("0", 15 - (SqlFunctions.StringConvert((double)mm.IDMUESTRA).Trim()).Length) 
            + SqlFunctions.StringConvert((double)mm.IDMUESTRA).Trim()
   ).ToList();
-2

All Articles