I have a list of products:
IQueryable<Product> list = _ProductRepository.GetAll();
The column Product.Clicks corresponds to the number of times the product has been viewed.
I want to update this list of products and update the column Product.Rank using a ranking such as this:
SELECT
ProductId,
Name,
RANK() OVER (ORDER BY Clicks DESC) [Rank],
Clicks
FROM
Product
WHERE
Clicks > 0
What is the most efficient way using LINQ to do this? Is there a way to do a direct update, rather than querying the products and enumerating them? This will run as a batch job on a weekly basis.
UPDATE: It appears that many people have suggested that a SQL SP batch job would be the best way forward here. Points go to the person that suggests such a query.
UPDATE: Answer as follows:
CREATE PROCEDURE UpdateRank
AS
BEGIN
SET NOCOUNT ON;
WITH RankValues AS
(SELECT [ProductId], [Clicks], [Rank],
RANK() OVER (ORDER BY [Clicks] DESC) AS [NewRank]
FROM [Product])
UPDATE [RankValues]
SET [Rank] = [NewRank]
WHERE Clicks > 0
END
GO