该类型不能被隐式转换为系统。到system.collections.generic.list< crypto>

发布于 2025-01-29 15:20:42 字数 792 浏览 5 评论 0原文

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&lt;&lt;匿名类型:字符串名称,int id,system.dateTime intement.datetime更新&gt;&gt;”到“ system.collections.generic.list&lt; cryptoapi.modules

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; }
}

Error message:

The type cannot be converted implicitly "System.Collections.Generic.List<<anonymous type: string name, int id, System.DateTime UpdateDate>>" to "System.Collections.Generic.List<CryptoAPI.Modules

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2025-02-05 15:20:42

您目前正在返回作为匿名类型的列表。

相反,指定.select()的类型

List<Crypto> cryptos = await _context.cryptocurrencies
    .Select(c => new Crypto
    {
        Name = c.Name,
        Id = c.Id,
        updateDate = c.updateDate
    })
    .ToListAsync();

,并建议使用 pascalcase (公共)属性的命名。

You are currently returning as a List of anonymous type.

Instead, specify the type in .Select()

List<Crypto> cryptos = await _context.cryptocurrencies
    .Select(c => new Crypto
    {
        Name = c.Name,
        Id = c.Id,
        updateDate = c.updateDate
    })
    .ToListAsync();

And would suggest using PascalCase naming for (public) properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文