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:
Here are 4 products.
Here are 9 products
And on my list that should be the different list contains 9 products and not 5
What is that
Supplier
has to do with the problem?– Maniero
I only managed what I have because I didn’t know if he could influence the outcome
– user93569
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>
– ramaral
@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
@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
– user93569