该类型不能隐式转换为<anonymous> System.Collections.Generic.List 到 System.Collections.Generic.List<Cry


public async Task<List<Crypto>> GetWithoutPrices()
{
List<Crypto> cryptos = await _context.cryptocurrencies.Select(c => new
{
name = c.Name,
id = c.Id,
UpdateDate = c.updateDate
}).ToListAsync();
return cryptos;
}
public class Crypto
{
public int Id { get; set; }
public decimal Price { get; set; }
public string Name { get; set; }
public DateTime updateDate { get; set; }
}

错误信息:

类型不能隐式转换"System. collections . generic . list <<匿名类型:字符串名称,int id, System。和"DateTime UpdateDate>;以"System.Collections.Generic.List<CryptoAPI.Modules

您当前返回的是一个匿名类型的List。

应该在.Select()

中指定类型。
List<Crypto> cryptos = await _context.cryptocurrencies
.Select(c => new Crypto
{
Name = c.Name,
Id = c.Id,
updateDate = c.updateDate
})
.ToListAsync();

建议对(公共)属性使用PascalCase命名。

相关内容

最新更新