Mapster
Mapster copied to clipboard
An exception occurs when Where is invoked after ProjectToType.
Hi: My code threw an exception after calling the Where method after the ProjectToType method. My codes:
var data = service.GetService<ICoreRepository>().CustomerVisits.Include("CustomerProject").ProjectToType<CustomerVisitModel>();
data = data.Where(p => p.CustomerProject.Id.Equals(Guid.NewGuid())).ToList().AsQueryable();
Exception Info: System.ArgumentException:“Method 'Boolean Equals(System.Guid)' declared on type 'System.Guid' cannot be called with instance of type 'System.Nullable`1[System.Guid]'”
Other codes
public abstract class BaseEntity
{
/// <summary>
/// 实体ID
/// </summary>
[Key]
public Guid ID { get; protected set; }
}
public class CustomerProjectEntity : BaseEntity
{
public virtual IList<CustomerVisitEntity> CustomerVisits { get; private set; } = new List<CustomerVisitEntity>();
}
public class CustomerVisitEntity : BaseEntity
{
[Required]
public virtual CustomerProjectEntity CustomerProject { get; private set; }
}
public interface ICoreRepository
{
DbSet<Entities.CustomerProjectEntity> CustomerProjects { get; }
DbSet<Entities.CustomerVisitEntity> CustomerVisits { get; }
}
public class CoreRepository : DbContext, ICoreRepository
{
public DbSet<Entities.CustomerProjectEntity> CustomerProjects { get; set; }
public DbSet<Entities.CustomerVisitEntity> CustomerVisits { get; set; }
}
public class CustomerProjectModel
{
public Guid Id { get; set; }
}
public class CustomerVisitModel
{
public Guid Id { get; set; }
public CustomerProjectModel CustomerProject { get; set; }
}
Hi @s455016457, have you tried materializing your list before calling Where()? Something like this:
data = data.ToList().Where(p => p.CustomerProject.Id.Equals(Guid.NewGuid())).AsQueryable();
Assuming materializing the result fixed the problem. Let me know if not.