How to mount SELECT in lambda C #?

4

Recently I asked for help to assemble a SELECT to bring the price of the products

How to mount SELECT

They gave me this solution that worked perfectly:

SELECT P.PROCODIGO, P.PRONOME, H.HISPRECO
FROM PRODUTO P
INNER JOIN HISTORICO H ON P.PROCODIGO = H.PROCODIGO
WHERE H.HISDATA = (SELECT MAX(HISDATA) FROM HISTORICO WHERE PROCODIGO = P.PROCODIGO)

But now I need to mount this same SELECT in lambda with C #, because I started using Entity. Can anyone help me?

Thank you!

    
asked by anonymous 16.02.2016 / 20:18

1 answer

5

It would look like this:

var produtos = db.Historicos
                 .Include(h => h.Produto)
                 .OrderByDescending(h => h.HistoricoData)
                 .GroupBy(h => h.ProdutoId)
                 .Select(group => group.FirstOrDefault())
                 .ToList();

In the Entity Framework you should not think like SQL. The way of doing things is different. The generated SQL will possibly not be the same, but the result will be.

    
16.02.2016 / 20:29