Expression does not bring results, but there is data

Asked

Viewed 72 times

2

This is my direct sql query.

select c.de_cnpj, c.DT_TransacaoV from T_CRM_StatusPDV c
join T_PDV p on c.DE_CNPJ = p.CNPJ
where DATEDIFF(DAY,c.DT_TransacaoV,GETDATE()) > 45

This is my lambda expression, which in my view corresponds to this query.

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

It turns out that the query brings me over 100 records, and with the lambda, the Count brings zero.

  • Try removing the Select() and the ToList() and assign the expression to a variable, for example: var sentenca = db.T_CRM_StatusPDV.Join(db.T_PDV, t1 => t1.DE_Cnpj, t2 => t2.CNPJ, (t1, t2) => new { t1, t2}).Where(dt => DbFunctions.DiffDays(dt.t1.DT_TransacaoV, DateTime.Now) > 45
 && dt.t2.DataCadastro >= dataInicio && dt.t1.DT_ControleV >= dataControle). See debug the value of sentenca. Which SQL is generated?

2 answers

2

solution:

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

0


I discovered the error. Lack of attention is fda. There is a && such and such, that this relationship is killing. Post solved. It lacked more observation on my part. It has to rise today and the pressure sometimes makes us not look at the obvious.

  • Publish the solution, it may help someone else.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.