LINQ to Entities does not recognize the method

0

I have a LINQ statement and use a static function in the where section. The static class is called Funciones but it always returns an error when I execute it, the description of the error is:

  

LINQ to Entities does not recognize the method 'System.Decimal Funcion MonedaMonto (System.Decimal, System.DateTime, Int32, Char)' method, and this method can not be translated into a store expression. "

How can I execute this function within LINQ?

Code:

var Query = (from TblInvCreditos in dbo.INVCREDITOS
                         join TblMovimientos in dbo.MOVIMIENTOS on TblInvCreditos.ID_CREDITO equals TblMovimientos.ID_CREDITO
                         where TblInvCreditos.ID_PERSONA == entidad.ID_PERSONA &&
                         !LstConvenio.Contains(TblInvCreditos.ID_CONVENIO) &&
                         TblInvCreditos.ACCESORIO == 0 &&
                         !LstIdStsSrc.Contains(TblInvCreditos.ID_STSCRD) &&
                         (Funciones.FuncionMonedaMonto(TblInvCreditos.I_CAPITAL + TblInvCreditos.I_INTERES + TblInvCreditos.I_SEGURO + TblInvCreditos.I_MORATORIO + TblInvCreditos.I_EROGA + ((decimal)(TblInvCreditos.I_MANTO.HasValue ? TblInvCreditos.I_MANTO.Value : 0)), TblInvCreditos.FCHFNLPROCESO.Value, (int)TblInvCreditos.ID_MONEDA, TblInvCreditos.ID_ZONA[0]) == 0)
                         select new
                         {
                             ID_CREDITO = TblInvCreditos.ID_CREDITO,
                             FCH_CREDIT = TblInvCreditos.FCH_CREDIT,
                             ACMSAR = TblInvCreditos.ACMSAR,
                             APL40 = TblMovimientos.ID_TRANSAC == 20 ? TblMovimientos.MONTO : 0,
                             APL60 = TblMovimientos.ID_TRANSAC == 20 ? TblMovimientos.MONTO : 0,
                             APL100 = TblMovimientos.ID_TRANSAC == 20 ? TblMovimientos.MONTO : 0
                         }
                         ).GroupBy(x => new { x.ID_CREDITO, x.FCH_CREDIT, x.ACMSAR }
                         ).Select(g =>
                               new {
                                   ID_CREDITO = g.Key.ID_CREDITO,
                                   FCH_CREDIT = g.Key.FCH_CREDIT,
                                   ACMSAR = g.Key.ACMSAR,
                                   APL40 = g.Sum(x => Math.Round(Convert.ToDecimal(x.APL40.Value), 2)),
                                   APL60 = g.Sum(x => Math.Round(Convert.ToDecimal(x.APL60.Value), 2)),
                                   APL100 = g.Sum(x => Math.Round(Convert.ToDecimal(x.APL100.Value), 2))
                               }
                         ).ToList();
    
asked by Manolo 27.04.2018 в 22:39
source

1 answer

0

The problem is that you are not using just Linq , you are using Entity Framework .

Normally linq executes the functions and processes the information, however when EF is involved, it takes the code and performs a static analysis to try to replace the functions of C # with those of SQL.

For example, in your ListConvenio.Contains , it would be very heavy to bring all the results and then check them against the list in the contains, then before making the query convert that contains into WHERE IN ... .

Then it is easy to understand that when you use LINQ and EF, you should consider what functions are put in the where, for example, if you want to do something with dates you are restricted from using them like this:

context.Tabla.Where(x => x.dateToCheck > DateTimeOffset.Today.AddHours(-1))

This would mark an error because EF does not know how to pass that DateTimeOffset to an SQL function, however doing so if it works:

var dateInWhere = DateTimeOffset.Today.AddHours(-1);
context.Tabla.Where(x => x.dateToCheck > dateInWhere)

This is because if you save how to put a DateTimeOffset object, it is no longer the function, it is the value itself.

    
answered by 01.05.2018 в 00:26