表达式转换为 linq 表达式时索引超出范围
我正在从我的 userGroupsRepository 创建选择列表项的集合。 我知道有两条记录。
我做错了什么吗?
首先,我编写了以下代码,因为我认为这是获取选择列表项集合的更快方法,其中我有“this._userGroupRepository.All”作为 IQueryable
我的集合是:
List<SelectListItem> listItems = this._userGroupRepository.All.Select(
userGroup => new SelectListItem() {
Text = userGroup.GroupName,
Value = userGroup.UserGroupId.ToString()
}).ToList();
但是此实现的结果是
索引超出范围。必须为非负数且小于 集合。参数名称:索引
,这里我有我的集合实现,并将其重写为 foreach
List<SelectListItem> listItems = new List<SelectListItem>();
foreach (UserGroup userGroup in this._userGroupRepository.All)
{
listItems.Add(new SelectListItem(){
Text = userGroup.GroupName,
Value = userGroup.UserGroupId.ToString()});
}
I am creating a collection of select list items from my userGroupsRepository.
I know that there are two records.
Is there something what i am doing wrong?
At first i have written following code as i thought this is a faster way to get my select list item collection, where i have "this._userGroupRepository.All" as IQueryable
my collection is:
List<SelectListItem> listItems = this._userGroupRepository.All.Select(
userGroup => new SelectListItem() {
Text = userGroup.GroupName,
Value = userGroup.UserGroupId.ToString()
}).ToList();
this implementation however results with
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index
and here i have my implementation of collection with rewriting it as foreach
List<SelectListItem> listItems = new List<SelectListItem>();
foreach (UserGroup userGroup in this._userGroupRepository.All)
{
listItems.Add(new SelectListItem(){
Text = userGroup.GroupName,
Value = userGroup.UserGroupId.ToString()});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对你的 Queryable 进行枚举有帮助吗?
Does it help to make an Enumerable of your Queryable?