首页 > 解决方案 > LINQ 查询在 FirstOrDefault 方法上引发异常

问题描述

我正在使用 EF 核心,并且两个实体之间存在多对多关系

IotaProject <--> 用户

这是与问题相关的实体和 dto

public class IotaProject
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string ProjectName { get; set; }
    [Required]
    public DateTime Create { get; set; }

    public ICollection<ProjectOwnerJoint> Owners { get; set; } = new List<ProjectOwnerJoint>();

}

public class ProjectOwnerJoint
{
    public int IotaProjectId { get; set; }
    public IotaProject IotaProject { get; set; }

    public int UserId { get; set; }
    public User User { get; set; }

}

public class User
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string FullName { get; set; }
    [Required]
    public string ShortName { get; set; }
    [Required]
    public string Email { get; set; }

    public ICollection<ProjectOwnerJoint> OwnedProjects { get; set; } = new List<ProjectOwnerJoint>();
}

public class ApplicationDbContext : DbContext
{
    public DbSet<IotaProject> IotaProjects { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<ProjectOwnerJoint> ProjectOwnerJoint { get; set; }
}

public class IotaProjectDisplayDto
{
    public int Id { get; set; }
    public string ProjectName { get; set; }
    public DateTime Create { get; set; }

    public UserMinDto Owner { get; set; }
    public int Count { get; set; }

    public IEnumerable<UserMinDto> Reviewers { get; set; }
}

public class UserMinDto
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string ShortName { get; set; }
}

以下是LINQ的问题,LINQ的目的是将IotaProject转换为IotaProjectDisplayDto,关键是IotaProject的Owners属性是ICollection,而IotaProjectDisplayDto中的Owner属性只是一个单元素UserMinDto,所以我只需要获取IotaProject的第一个元素所有者,那就是 FirstOrDefault() 来了。

IEnumerable<IotaProjectDisplayDto> results = _db.IotaProjects.Select(x => new IotaProjectDisplayDto
            {
                Id = x.Id,
                ProjectName = x.ProjectName,
                Create = x.Create,
                Owner = x.Owners.Select(y => y.User).Select(z => new UserMinDto { Id = z.Id, FullName = z.FullName, ShortName = z.ShortName }).FirstOrDefault()
            });
            return results;

它抛出运行时异常

Expression of type 'System.Collections.Generic.List`1[ToolHub.Shared.iota.UserMinDto]' cannot be used for parameter 
of type 'System.Linq.IQueryable`1[ToolHub.Shared.iota.UserMinDto]' 
of method 'ToolHub.Shared.iota.UserMinDto FirstOrDefault[UserMinDto](System.Linq.IQueryable`1[ToolHub.Shared.iota.UserMinDto])' (Parameter 'arg0')

我猜这可能与延迟执行有关,但是在阅读了一些帖子后,我仍然无法解决。

任何提示将不胜感激。

现在,我可以完成这项工作的唯一方法是将 IotaProjectDisplayDto 中的 Owner 属性类型更改为 IEnumrable,这将不再需要 FirstOrDefault() 来立即执行。后来,我手动让客户端中的第一个元素显示。

标签: entity-framework-corelinq-to-entities

解决方案


此问题发生在 Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview7.19362.6

正如 Ivan 在评论中建议的那样,我最终降级到 EF core stable 2.2.6,一切正常。


推荐阅读