gpt4 book ai didi

c# - 参数类型 System.Linq.IQueryable<> 不可分配给参数类型 Systems.collection.generic.ienumerable

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:08 25 4
gpt4 key购买 nike

我正在尝试连接表中的两个字段并将其分配为列表。请看下面的代码

public List<ForDropDownList> GetAccountsForDownList()
{
var accountList = new List<ForDropDownList>();

using (_context = new ApplicationContext())
{
accountList.AddRange( _context.Account
.Select(a => new
{
a.AccountId,
FullName = string.Concat(a.FirstName, a.LastName)
}));
}

return accountList;
}

这是我的模型

public class ForDropDownList
{
public int AccountId { get; set; }
public string FullName { get; set; }
}

我收到这个错误:

Argument type System.Linq.IQueryable<{AccountId:int, FullName:string}> is not assignable to parameter type Systems.Collection.Generic.IEnumerable

最佳答案

基本上,您的选择会实例化一个新的动态对象,然后您无法将其添加到您的 List<ForDropDownList> 中以下代码应该有效:

accountList.AddRange(_context.Account.Select(a => new ForDropDownList { AccountId = a.AccountId, FullName = string.Concat(a.FirstName, a.LastName) }));

关于c# - 参数类型 System.Linq.IQueryable<> 不可分配给参数类型 Systems.collection.generic.ienumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353407/

25 4 0