>' 到 'System.Collections.Generic.IEnumerable',c#,asp.net-mvc,entity-framework,entity-framework-core"/>

首页 > 解决方案 > 无法隐式转换类型'System.Collections.Generic.List<>' 到 'System.Collections.Generic.IEnumerable'

问题描述

我想在函数中返回字符串列表:

private async Task<IEnumerable<string>> GetAccessLevels(Guid roleId)
{
    return await AccessLevels.Where(x => x.RoleId == roleId).Select(x=>new {x.Access }).ToListAsync();
}

但它告诉我这个错误:

无法将类型隐式转换'System.Collections.Generic.List<<anonymous type: string Access>>''System.Collections.Generic.IEnumerable<string>'. 存在显式转换(您是否缺少演员表?)

这是我的模型:

public Guid RoleId { get; set; }
public string Access { get ; set; }

我怎么解决这个问题?

标签: c#asp.net-mvcentity-frameworkentity-framework-core

解决方案


只需更改此:

Select(x => new { x.Access })

对此:

Select(x => x.Access)

原因是,new让您的查询返回一个IEnumerableof Anonymous types,而您需要一个IEnumerableof Strings


推荐阅读