簡體   English   中英

如何將此SQL查詢[選擇MAX(id)]更改為linq

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

如何在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

有任何想法嗎?

謝謝

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

使用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};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM