MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.31k stars 328 forks source link

An exception occurs when Where is invoked after ProjectToType. #408

Closed s455016457 closed 1 year ago

s455016457 commented 2 years ago

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; }
}
andrerav commented 2 years ago

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();
andrerav commented 1 year ago

Assuming materializing the result fixed the problem. Let me know if not.