How to IEnumerable query in ASP.NET Core MVC?

I have a quick question. I have a table named categories and i want to list as SelectListItem. So far I tried with this code:

_context.Categories.ToList();
_context.Categories.AsIEnumerable();

but no luck.

How do I query my table as IEnumerable?

Upvotes: 0

Views: 466

Answers (1)

Serge
Serge

Reputation: 43929

Try this code:

var selectListItems= _context.Categories
.Select(i=> new SelectListItem {
Value=i.Id.ToString(),
Text=i.Name
}).ToList(); // or .ToArray() it will be a little more sufficient As enumerable

Upvotes: 1

Related Questions