Mauro Petrini
Mauro Petrini

Reputation: 351

how do I change this sql query [Select MAX(id)] to linq

How can I do this in LINQ?

SELECT MAX(ptc.idProducto_Talle_Color), t.idTalle, t.Numero 
FROM Producto_Talle_Color ptc INNER JOIN Talle t ON ptc.Talle_idTalle = t.idTalle
WHERE ptc.Producto_idProducto = 3
GROUP BY t.idTalle, t.Numero

Any ideas?

Thanks

Upvotes: 0

Views: 124

Answers (2)

Mehdi Haghshenas
Mehdi Haghshenas

Reputation: 2447

with lambda expression your answer is:

var res=context.Talle.Select(t=>
t.idTalle, 
t.Numero,
t.Producto_Talle_Color.Max(ptc=>ptc.idProducto_Talle_Color)
).Where(t=>t.Producto_Talle_Color.Producto_idProducto == 3);

Upvotes: 1

Mehdi Haghshenas
Mehdi Haghshenas

Reputation: 2447

with linq your answer is:

var res = from y in (
from ptc in context.Producto_Talle_Color
 where ptc.Producto_idProducto == 3  
group ptc by ptc.Talle_idTalle 
into grouedres select 
new {max=grouedres.Max(x=>x.idProducto_Talle_Color),id=g.Key}
)
 join t in context.Talle on y.id equals t.idTalle 
select {max,t.idTalle, t.Numero};

Upvotes: 0

Related Questions