I have a LINQ select query which fetches a list of books:
var books = from b in _context.Books
where b.Available && b.BookType == bookType
orderby b.Title
select new BookViewModel
{
Title = b.Title,
Description = b.Description,
Id = b.Id,
SortOrder = ?
};
BookViewModel
has a SortOrder
property which I want to set in the query. The first book in the list would have SortOrder = 1
, the second book would have 2, etc. It's essentially just the index of the book in the returned list.
Is there a way to set this within the query? I understand I could do another linq query once I have the list, but this seems inefficient. I don't properly understand the innards of a linq select, but given it's sorted by the orderby I don't see why there wouldn't exist some way to set the index of the element.