Here is a recursive function that you can call to find out the line number. If your entries in the database change frequently, this probably won't work, as it calls up a database that narrows the search in half several times each time.
public static int FindRowNumber<T>(IQueryable<T> query, Expression<Func<T, bool>> search, int skip, int take) { if(take < 1) return -1; if(take == 1) return query.Skip(skip).Take(take).Any(search) ? skip : -1; int bottomSkip = skip; int bottomTake = take / 2; int topSkip = bottomTake + bottomSkip; int topTake = take - bottomTake; if(query.Skip(bottomSkip).Take(bottomTake).Any(search)) { return FindRowNumber(query, search, bottomSkip, bottomTake); } if(query.Skip(topSkip).Take(topTake).Any(search)) { return FindRowNumber(query, search, topSkip, topTake); } return -1; }
You call it this:
var query = ... //your query with ordering and filtering int rownumber = FindRowNumber(query, x => x.Record.ID == 35, 0, query.Count());
source share