[英]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.