© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
16 replies
BigggMoustache

✅ IEnum and IQueryable usage.

My Controller action is here.
[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
decksIQ
decksIQ
is equal to the filtered down
decks
decks
Enum after processing the first line, but the third line when passing
decksIQ
decksIQ
in the parameter says it's the full unfiltered list again. Why would this happen?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ IQueryable implementation
C#CC# / help
3y ago
Entity Framework IQueryable question
C#CC# / help
2y ago
❔ [.NET 6][Odata 8] IQueryable Async
C#CC# / help
4y ago
✅ LINQ help: IQueryable<Entity> -> Dictionary<string, Entity[]>
C#CC# / help
12mo ago