How can I convert the following T-SQL query, that produces correct results, to LINQ expression in C#?
WITH CTE
AS ( SELECT RN = ROW_NUMBER() OVER ( PARTITION BY dbo.Product.ID ORDER BY dbo.Picture.UpdatedDate ASC )
,Product.ID
,Product.Name
,Picture.Path
FROM Dbo.Product
INNER JOIN dbo.Product_Picture_Mapping
ON dbo.Product_Picture_Mapping.ProductID = dbo.Product.ID
INNER JOIN dbo.Picture
ON dbo.Picture.ID = dbo.Product_Picture_Mapping.PictureID)
SELECT ID AS 'ID'
,Name AS 'Name'
FROM CTE
WHERE RN = 1
I have tried the following code.
var result= (from c in cd.Product
join pp in cd.Product_Picture_Mapping
on c.ID equals pp.ProductID
join pcc in cd.Picture
on pp.PictureID equals pcc.ID
select new ProductViewModel() { PicturePath = pcc.Path, ProductName = c.Name, ProductID = c.ID}).ToList();