开发者

LINQ - join the highest rank picture of a product that has many pictures

The query below gets all customers and the products they order. There are multiple pictures per product, so the query returns duplicate rows. I only want one of the pictures to prevent this duplication. The pictures have开发者_JAVA技巧 a rank order. So, I need to sort the pictures and take the one with highest rank and join that to products. How can I do this?

from c in Customers 
join o in OnlineOrders on c.CustomerID equals o.CustomerID into ords
from co in ords.DefaultIfEmpty() 
join pro in Products on co.OnlineOrderID equals pro.OnlineOrderID into oprods
from op in oprods.DefaultIfEmpty()
join pict in Pictures on op.ProductID equals pict.ProductID into picpros 
from ppro in picpros.DefaultIfEmpty()
select new { 
c.CustomerName, co.OnlineOrderTitle,  
op.ProductTitle, ppro.PictureFilename }


I figured it out.

from ppro in picpros.DefaultIfEmpty().Take(1) 




var result=db.OnlineOrders.Select(p=>
new {
 CustomerName=p.Customer.CustomerName,
 OnlineOrderTitel=p.OnlineOrderTitel,
 ProductTitel=p.Protuct.ProductTitel,
 PictuteFileName=p.Procuct.Pictures.OrderByDescending(u=>u.Rank).First()) });



I guess Product has one to many OnlineOrder relation, if not(namy to many) I have to know your structure. Do you have "duplicates" in OnlineOrder table or OnlineOrderProduct table?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜