2014-09-12 20 views
0

如何在LINQ中執行此操作?如何更改此sql查詢[Select MAX(id)] 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 

任何想法?

感謝

+1

這裏的東西,你可以看看,並應用到您現有的代碼,我會建議在google搜索方面多付出一些努力http://msdn.microsoft.com/en-us/library/vstudio/bb386922(v=vs.100).aspx – MethodMan 2014-09-12 19:41:09

回答

0

linq你的答案是:

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}; 
1

lambda expression你的答案是:

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);