Error calculating difference between dates in Entity with Linq to Entities

3

Before this was my expression:

var resultado = db.T_CRM_StatusPDV
                              .Join(db.T_PDV, t1 => t1.DE_Cnpj, t2 => t2.CNPJ, (t1, t2) => new { t1, t2})
                              .Where(dt => (TimeSpan)((dt.t1.DT_TransacaoV - DateTime.Now)).TotalDays > 45
                                     && dt.t2.DataCadastro >= dataInicio && dt.t1.DT_ControleV >= dataControle)
                              .Select(i => new { i.t1.DE_Cnpj }).ToList();

This caused this error:

DbArithmeticExpression arguments must have a numeric common type.

I changed to this expression:

var resultado = db.T_CRM_StatusPDV
                              .Join(db.T_PDV, t1 => t1.DE_Cnpj, t2 => t2.CNPJ, (t1, t2) => new { t1, t2})
                              .Where(dt => EntityFunctions.DiffHours(dt.t1.DT_TransacaoV,DateTime.Now) > 45 
                                     && dt.t2.DataCadastro >= dataInicio && dt.t1.DT_ControleV >= dataControle)
                              .Select(i => new { i.t1.DE_Cnpj }).ToList();

And now I have this error:

LINQ to Entities does not recognize the method 'System.Nullable'1[System.Int32] DiffHours(System.Nullable'1[System.DateTime], System.Nullable'1[System.DateTime])' method, and this method cannot be translated into a store expression.

How can I calculate time in a lambda expression or linq to entities?

    
asked by anonymous 06.06.2014 / 21:10

1 answer

1

Pro error first, switch:

.Where(dt => (TimeSpan)((dt.t1.DT_TransacaoV - DateTime.Now)).TotalDays > 45

By:

.Where(dt => DbFunctions.DiffDays(dt.t1.DT_TransacaoV, DateTime.Now) > 45

On the second, try switching EntityFunctions to DbFunctions .

    
06.06.2014 / 21:14