c# - 无法隐式转换类型'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; }
我怎么解决这个问题?
解决方案
只需更改此:
Select(x => new { x.Access })
对此:
Select(x => x.Access)
原因是,new
让您的查询返回一个IEnumerable
of Anonymous types
,而您需要一个IEnumerable
of Strings
。
推荐阅读
- missing-data - 交叉表包括缺失值
- java - 由于 ASYNC_NETWORK_IO 在 java 代码中出现 DB IO 问题
- hosting - 如何在 CloudFlare 更新 CName 记录
- c++ - 用户回答选项显示数字选择而不是数字是什么?C++
- android - 使用 openfire -Android 更新和检索聊天记录
- javascript - 如何忽略javascript中的参数?
- angular - RxJS combineLatest 在更新 observable 后没有正确发射
- node.js - nodejs TRX gettransactionstothis 不起作用
- sql-server - 从一个地方远程访问另一个 MS SQL SERVER
- php - 不熟悉php中的数组函数术语