✅ IEnum and IQueryable usage.
My Controller action is here.
The pagination example from docs uses IQueryable so I was doing this to get the IEnum from the sorting part above to work with it.
It says
[AllowAnonymous]
public async Task<IActionResult> Index(IndexViewModel model, string? filter, SortOption? sort, int? pageIndex)
{
var decks = _mtgDbContext.Decks.AsEnumerable();
var decksIQ = _mtgDbContext.Decks.AsQueryable();
if (!string.IsNullOrEmpty(filter))
{
decks = decks.Where(t => t.Name.Contains(filter, StringComparison.OrdinalIgnoreCase));
}
if (sort is { } s)
{
decks = s switch
{
SortOption.NameDesc => decks.OrderByDescending(t => t.Name),
SortOption.NameAsc => decks.OrderBy(t => t.Name),
_ => decks.OrderBy(t => t.Name)
};
}
var deckIQ = decks.AsQueryable();
var pageSize = _configuration.GetValue("PageSize", 3);
var paginatedList = await PaginatedList<Deck>.CreateAsync(decksIQ, pageIndex ?? 1, pageSize);
model.Decks = paginatedList;
model.Filter = filter;
model.Sort = sort;
return View(model);
}[AllowAnonymous]
public async Task<IActionResult> Index(IndexViewModel model, string? filter, SortOption? sort, int? pageIndex)
{
var decks = _mtgDbContext.Decks.AsEnumerable();
var decksIQ = _mtgDbContext.Decks.AsQueryable();
if (!string.IsNullOrEmpty(filter))
{
decks = decks.Where(t => t.Name.Contains(filter, StringComparison.OrdinalIgnoreCase));
}
if (sort is { } s)
{
decks = s switch
{
SortOption.NameDesc => decks.OrderByDescending(t => t.Name),
SortOption.NameAsc => decks.OrderBy(t => t.Name),
_ => decks.OrderBy(t => t.Name)
};
}
var deckIQ = decks.AsQueryable();
var pageSize = _configuration.GetValue("PageSize", 3);
var paginatedList = await PaginatedList<Deck>.CreateAsync(decksIQ, pageIndex ?? 1, pageSize);
model.Decks = paginatedList;
model.Filter = filter;
model.Sort = sort;
return View(model);
}The pagination example from docs uses IQueryable so I was doing this to get the IEnum from the sorting part above to work with it.
var deckIQ = decks.AsQueryable();
var pageSize = _configuration.GetValue("PageSize", 3);
var paginatedList = await PaginatedList<Deck>.CreateAsync(decksIQ, pageIndex ?? 1, pageSize); var deckIQ = decks.AsQueryable();
var pageSize = _configuration.GetValue("PageSize", 3);
var paginatedList = await PaginatedList<Deck>.CreateAsync(decksIQ, pageIndex ?? 1, pageSize);It says
decksIQdecksIQ is equal to the filtered down decksdecks Enum after processing the first line, but the third line when passing decksIQdecksIQ in the parameter says it's the full unfiltered list again. Why would this happen?