ValeraT1982 / ObjectsComparer

C# Framework provides mechanism to compare complex objects, allows to override comparison rules for specific properties and types.
MIT License
352 stars 86 forks source link

Compare two objects with List properties #26

Closed leandrobattochio closed 3 years ago

leandrobattochio commented 3 years ago

How can I compare two objects that has a List property and use only the ID of that list to compare the differences? I read the documentation and couldnt find anything that solves my problem.

ValeraT1982 commented 3 years ago

Can you please provide an example of your code? Probably you'll need a custom comparer. Look at this example.

leandrobattochio commented 3 years ago

This is my entities:

public class Fornecedor : Entity,
        IViewModel<FornecedorViewModel, Fornecedor>
    {
        public Fornecedor(string nome, string descricao, IList<Produto> produtos)
        {
            Nome = nome;
            Descricao = descricao;
            Produtos = produtos;
        }

        public Fornecedor(Guid id, string nome, string descricao, IList<Produto> produtos)
        {
            Id = id;

            Nome = nome;
            Descricao = descricao;
            Produtos = produtos;
        }

        public Fornecedor()
        {

        }

        [Required]
        public string Nome { get; private set; }
        public string Descricao { get; private set; }

        [Required]
        public virtual IList<Produto> Produtos { get; set; }
    }

    public class Produto : Entity,
        IViewModel<ProdutoViewModel, Produto>
    {
        public Produto(string nome, string descricao, IList<PrecoVariavel> listaDePreco)
        {
            Nome = nome;
            Descricao = descricao;
            ListaDePreco = listaDePreco;
        }

        public Produto(Guid id)
        {
            Id = id;
        }

        public Produto(Guid id, string nome, string descricao, IList<PrecoVariavel> listaDePreco)
        {
            Id = id;
            Nome = nome;
            Descricao = descricao;
            ListaDePreco = listaDePreco;
        }

        protected Produto()
        {

        }

        [Required]
        public string Nome { get; private set; }
        public string Descricao { get; private set; }

        public virtual IList<PrecoVariavel> ListaDePreco { get; set; }
    }

By comparing Fornecedor, I want to know which "Produto" has been modified. I want to compare the ID only of Produto, discarding all the other properties. I tried this example you provided but I cannot make it work. Maybe I'm missing something?

ValeraT1982 commented 3 years ago

What do you mean by "compare the ID only of Produto"? If object 1 and object 2 have product with id=1 but with different Nome/Descricao/ListaDePreco do you want comparer to see this objects as equal? What if object 1 and object 2 have products with id=1 and id=2 but in different order?

Can you please provide code of your custom comparer and describe issue you have?