简体   繁体   English

LINQ查询在FirstOrDefault方法上引发异常

[英]LINQ query throw exception on FirstOrDefault method

I'm using EF core, and I have a many-to-many relationship between two entity 我使用的是EF核心,并且两个实体之间存在多对多关系

IotaProject <--> User IotaProject <->用户

Here's entities & dto related to the question 这是与问题相关的实体和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; }
}

Following LINQ is the problem, the LINQ purpose is to convert IotaProject to IotaProjectDisplayDto, and key part is that Owners property of IotaProject is ICollection and Owner property in IotaProjectDisplayDto is just one single element UserMinDto , so I only need to get the first element of IotaProject's Owners and that's FirstOrDefault() comes. 接下来的问题是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;

it throws run-time exception 抛出运行时异常

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')

I'm guessing it's probably related to deferred execution, but after read some posts, I still can't resolve it. 我猜这可能与延迟执行有关,但是在阅读了一些帖子之后,我仍然无法解决它。

Any tips would be appreciated. 任何提示将不胜感激。

Right now, the only way I can get this work is I change type of Owner property in IotaProjectDisplayDto into IEnumrable, which will no longer need FirstOrDefault() to immediate execution. 现在,我可以完成这项工作的唯一方法是将IotaProjectDisplayD中的Owner属性的类型更改为IEnumrable,这将不再需要FirstOrDefault()来立即执行。 And later on, I manually get the first element in the client to display. 之后,我手动获取客户端中的第一个元素以显示。

This issue happened in Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview7.19362.6 此问题发生在Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview7.19362.6中

I end up downgrade to EF core stable 2.2.6 as Ivan suggested in comment, and everything works fine. 正如Ivan在评论中建议的那样,我最终降级到EF核心稳定版2.2.6,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ C# 现有元素的 FirstOrDefault() 查询返回 null - LINQ C# FirstOrDefault() query return null for the existing element 在子查询中使用 FirstOrDefault - Using FirstOrDefault in a sub query 升级到 .NET Core 3.1 并在与 using.FirstOrDefault() 相关的 LINQ 查询中收到错误 - Upgraded to .NET Core 3.1 and receiving an error in a LINQ query related to using .FirstOrDefault() 为什么在 linq 表达式的 where 子句中调用局部变量时会引发此异常? - Why does this exception throw when calling a local variable in a where clause of a linq expression? 在LINQ查询上使用“包含”方法时出错 - Error when using “Include” method on a LINQ Query 创建一个包含linq查询的异步方法 - Creating an asynchronous method that contains a linq query LINQ查询的一部分,用于分离方法(EF Core) - Part of LINQ query to seperate method (EF Core) EF7 / Linq-包含嵌套对象的“包含”查询引发异常 - EF7 / Linq - “Contains” query with nested object throws Exception LINQ 查询异常错误无效的列名 &#39;courseId1&#39; EFCore - LINQ Query Exception error Invalid column name 'courseId1' EFCore InvalidOperationException:尝试评估 LINQ 查询参数表达式时引发异常 - InvalidOperationException: An exception was thrown while attempting to evaluate a LINQ query parameter expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM