简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM