简体   繁体   English

如何在 ASP.NET Core MVC 中进行 IEnumerable 查询?

[英]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 .我有一个名为 categories 的表,我想列为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 ?如何将我的表查询为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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