Conversion error in Where clause when filtering records using EF Core 2.0

Asked

Viewed 27 times

0

I’m getting a list of records and filtering according to the given parameter. The problem is that an error is appearing in which I do not know (as picture). There are two classes : Personal Document and Documenttype... Where, when obtaining a list of documents, one should bring the related types of documents.

What is wrong?

public IQueryable<PessoaDocumento> GetPaginated(string filter, int initialPage, int pageSize, out int totalRecords, out int recordsFiltered, int pessoaId, bool @readonly = false)
{
    var data = DbSet
       .Include(x => x.DocumentoTipo)
       .Where(x => x.PessoaId == pessoaId)
       .AsNoTracking().AsQueryable();

    totalRecords = data.Count();

    if (!string.IsNullOrEmpty(filter))
    {
        data = data.Where(x =>
         !string.IsNullOrEmpty(x.NumeroDocumento) ? x.NumeroDocumento.Contains(filter.ToUpper()) : false
         || x.DocumentoTipo != null ? x.DocumentoTipo.Descricao.Contains(filter.ToUpper()) : false
         || x.DocumentoTipo != null ? x.DocumentoTipo.Sigla.Contains(filter.ToUpper()) : false
        );
    }

    recordsFiltered = data.Count();

    if (recordsFiltered == 0)
        totalRecords = 0;

    data = data
        .Skip(initialPage)
        .Take(pageSize);

    return data;
}


public class PessoaDocumento : Entity
{
    public int Id { get; set; }

    public int DocumentoTipoId { get; set; }

    private string numeroDocumento;
    public string NumeroDocumento
    {
        get { return numeroDocumento; }
        set
        {
            if (value != null)
            {
                numeroDocumento = value.Trim().ToUpper();
            }
        }
    }   
    
     public virtual DocumentoTipo DocumentoTipo { get; private set; }
}



public class DocumentoTipo : Entity
{
    private string codigoControle;
    public string CodigoControle
    {
        get { return codigoControle; }
        set
        {
            if (value != null)
                codigoControle = value.ToUpper().Trim();
            else
                codigoControle = value;
        }
    }

    private string descricao;
    public string Descricao
    {
        get { return descricao; }
        set
        {
            if (value != null)
                descricao = value.ToUpper().Trim();
            else
                descricao = value;
        }
    }

    private string sigla;
    public string Sigla
    {
        get { return sigla; }
        set
        {
            if (value != null)
                sigla = value.ToUpper().Trim();
            else
                sigla = value;
        }
    }
    
    public virtual ICollection<PessoaDocumento> PessoasDocumentos { get; private set; }
}


public abstract class Entity
{
    //public Guid Id { get; protected set; }
    public int Id { get; protected set; }

    public override bool Equals(object obj)
    {
        var compareTo = obj as Entity;

        if (ReferenceEquals(this, compareTo)) return true;
        if (ReferenceEquals(null, compareTo)) return false;

        return Id.Equals(compareTo.Id);
    }

    public static bool operator ==(Entity a, Entity b)
    {
        if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
            return true;

        if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
            return false;

        return a.Equals(b);
    }

    public static bool operator !=(Entity a, Entity b)
    {
        return !(a == b);
    }

    public override int GetHashCode()
    {
        return (GetType().GetHashCode() * 907) + Id.GetHashCode();
    }

    public override string ToString()
    {
        return GetType().Name + " [Id=" + Id + "]";
    }
}

inserir a descrição da imagem aqui

  • 1

    data = data.Where(.. I think I should create another object, not assign it to the date itself. Besides, I think I should return a list there, I think I lack at the end a .ToList();

  • Hello @Ricardo Punctual! Even following your tips, the error persists! This seems to be some problem of EF Core 2.0. :(

  • By error msg, it looks like a conversion problem between the Id fields of the classes, as they are inherited from the Entity class...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.