We do have a database with thousand of entries. we would like to get the "ranked position" for a specific item by its name.
Since there is a lot of data we would like to avoid bring query ALL data in order to determine row index (using ToList() and IndexOf() for instance).
I tried using
List<Ranking> ranking = _context.Testes
.OrderByDescending(t => t.Val)
.Select((d, i) => new Ranking() {
Name = d.Name,
Ranking= i
}).First(d=>d.Name = "Test");
but I got this error:
'value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[WebApplication4.Models.Teste]).OrderByDescending(t => t.Val).Select((d, i) => new Ranking() {Name = d.Name, Ranking = i})': This overload of the method 'System.Linq.Queryable.Select' is currently not supported.
Is that possible somehow?
Select()overload to SQL. ORMs aren't meant for reporting and this is 100% a reporting query. SQL Server offers ranking functions likeROW_NUMBER,RANKandDENSE_RANK. You could create a view that calculates the rank and map your rankings to it, egCREATE VIEW Rankings AS SELECT Name,RANK OVER(ORDER BY Val) Ranking From Tests. EF will probably map theRankingclass to theRankingsview by convention. If not, you map it using theTableattribute