gpt4 book ai didi

c# - EF Core 无法从匿名类型的查询选择转换为 DTO 对象类

转载 作者:行者123 更新时间:2023-12-01 23:25:18 24 4
gpt4 key购买 nike

我无法将自定义查询的结果转换为 DTO 对象列表。我正在使用 EF Core 5.0.0。

这是我的 DTO 类:

public class UserPoolDto
{
public long Id { get; set; }
public string Name { get; set; }
public string Key { get; set; }
public string TypeName { get; set; }
public string CategoryName { get; set; }
public string StyleName { get; set; }
public decimal RegCost { get; set; }
public DateTime RegStartTimeUTC { get; set; }
public DateTime RegEndTimeUTC { get; set; }
public string Icon { get; set; }
public string Description { get; set; }
public string Instructions { get; set; }
public string CreatedBy { get; set; }
}

UserPoolDto 在 View 模型中用作简单列表,如下所示:

public class UserPoolVm
{
public IList<UserPoolDto> Pools { get; set; }
}

下面是使用 EF 核心查询数据库并尝试将结果转换为要分配给我的 View 模型的 UserPoolDto 对象列表的代码:

    public async Task<UserPoolVm> Handle(GetUserPoolsQuery request, CancellationToken cancellationToken)
{
var pools = await _context.Pools
.Include(p => p.Type)
.Include(p => p.Type.Category)
.Include(p => p.Style)
.Select(p => new
{
p.Id,
p.Name,
p.Key,
TypeName = p.Type.Name,
CategoryName = p.Type.Category.Name,
StyleName = p.Style.Name,
p.RegCost,
p.RegStartTimeUTC,
p.RegEndTimeUTC,
p.Icon,
p.Description,
p.Instructions,
p.CreatedBy,
})
.Where(x => x.CreatedBy == request.UserId)
.ToListAsync(cancellationToken);

return new UserPoolVm
{
Pools = pools // ERROR IS THROWN ON THIS LINE
};
}

但是,该项目不会生成,我收到以下错误(我将属性类型分成单独的行,以便更容易与上面的类属性进行比较):

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: long Id, string Name, string Key, string TypeName, string CategoryName, string StyleName, decimal RegCost, System.DateTime RegStartTimeUTC, System.DateTime RegEndTimeUTC, string Icon, string Description, string Instructions, string CreatedBy
to 'System.Collections.Generic.IList<Application.Pools.Queries.GetUserPools.UserPoolDto>'.
An explicit conversion exists (are you missing a cast?)

我试过像这样添加一个显式转换:

        return new UserPoolVm
{
Pools = (IList<UserPoolDto>)pools
};

这允许项目构建和运行,但我得到与上面完全相同的错误作为运行时错误,而不是阻止构建的错误。

我已经多次检查以确保返回的所有字段的数量、类型和名称与 UserPoolDto 匹配。这一切都匹配。我什至可以调试代码并确认查询返回的对象与 DTO 对象匹配。但是它仍然不会转换结果。

问题:谁能帮我弄清楚为什么查询返回的匿名类型无法转换为 UserPoolDto,即使所有属性都匹配?

谢谢

最佳答案

当编译器遇到 anonymous type它在内部创建一个名称自动生成的类。因此,即使您的匿名类型和 UserPoolDto dto 具有相同的属性,它们也是两种不同的类型或类。因此,当我们尝试将一个分配给另一个并尝试构建它时,最终会导致强制转换异常。

如所讨论的那样,有一些方法可以通过转换来解决手头的问题 here .然而,最好的方法是直接在您的选择中创建一个 dto 实例。

var pools = await _context.Pools
.Include(p => p.Type)
.Include(p => p.Type.Category)
.Include(p => p.Style)
.Select(p => new UserPoolDto
{
Id = p.Id,
Name = p.Name,
Key = p.Key,
TypeName = p.Type.Name,
CategoryName = p.Type.Category.Name,
StyleName = p.Style.Name,
RegCost = p.RegCost,
RegStartTimeUTC = p.RegStartTimeUTC,
RegEndTimeUTC = p.RegEndTimeUTC,
Icon = p.Icon,
Description = p.Description,
Instructions = p.Instructions,
CreatedBy = p.CreatedBy,
})
.Where(x => x.CreatedBy == request.UserId)
.ToListAsync(cancellationToken);

关于c# - EF Core 无法从匿名类型的查询选择转换为 DTO 对象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67347674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com