Compare two lists of objects and pick only the different ones

Asked

Viewed 4,757 times

3

I’m having trouble comparing two lists of the same kind List<Produto> in my product class I have the following structure.

public class Product
    {
        public int IdProduct { get; set; }
        public String Name { get; set; }

        public Supplier supplier { get; set; }

        public Product(){}
    }

And in my class of Supplier have:

public class Supplier
{
    public int IdSupplier { get; set; }
    public string Main { get; set; }
    public string Name { get; set; }
}

And I get an initial list, and an ending. The final contains more product elements.

Testing

List<Product> listaProdutosDiferentes = listaPrincipalProdutos.Union(listaFinalProdutos).ToList();

However this test only concatenated the two lists and not taking forming a list with products different from each other.

Editing

I have two lists, the first has some products may be (id;name) 1, Motorola One Mobile Phone; 2, Motorola G6 Cellular; 3, Motorola E5 cell phone;

On my second list you will have the same list as the first (Not necessarily following a sequence).

5, Samsung Galaxy Cell Phone; 1, Motorola One Mobile Phone; 2, Motorola G6 Cellular; 3, Motorola E5 Cellular; 4, Cellular Iphone 6S;

My final list needs to be

1, Motorola One Mobile Phone; 2, Motorola G6 Cellular; 3, Motorola E5 Cellular; 4, Cellular Iphone 6S; 5, Samsung Galaxy Cell Phone;

Why do I do that? so that when a person goes to make a search in my system, the system presents first the research that the client made, example "Motorola Cellular", and then the other items that have a relationship, in this case other cell phones.

[Duplicate]

Marked as duplicate, but in the answer that left not solved my problem I did the proposed codes, however all do not exclude the item that already has in the other list, I will show by images: IDE mostrando execução

Here are 4 products.

IDE mostrando execução

Here are 9 products

IDE mostrando execução

And on my list that should be the different list contains 9 products and not 5

  • 1

    What is that Supplier has to do with the problem?

  • I only managed what I have because I didn’t know if he could influence the outcome

  • Do not forget that the Product class should provide its own implementation of the Gethashcode() and Equals() methods. Or use, in the Except() method, a "Products Comparator" that implements the Iequalitycomparer<T interface>

  • @Leandropenhalver The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

  • @Maniero look man, I could not do as you made the proposal, then I did as it gave, I played this to deal with sql

1 answer

3

You must use the Except() of LINQ, which I use joins the lists without distinguishing what is already in the other.

var listaProdutosDiferentes = listaPrincipalProdutos.Except(listaFinalProdutos);

On the other hand I may want this (I have no way to say, the question is a little ambiguous, see which produces the result you expect):

var listaProdutosDiferentes = listaPrincipalProdutos.Except(listaFinalProdutos).Concat(listaFinalProdutos.Except(listaPrincipalProdutos));

Or still use the Intersect():

var listaProdutosDiferentes = listaPrincipalProdutos.Intersect(listaFinalProdutos);

I put in the Github for future reference.

Another point is that you should only use ToList() when you really need to make the list, it may even be your case, I have no way of disputing that, but it may not be necessary, so I chose not to use.

  • So I need it to be a list, because I at the end of the code I return a product-type list.

  • The code you made up there didn’t work, I need to compare the two lists and then I need this LINQ return me only the products that I don’t have in the first list.

  • Almost always people think they need to convert el list and do not need, often the error is in the API created. But I do not have to state in this case because it has no information on. I gave another option, if none of this resolve the question is not clear even.

  • Maniero, I added the question a few results to explain better what I want and why I’m trying to do it this way, if there’s another way to solve it if not in Brasilia there’s no problem too.

Browser other questions tagged

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