How to compare two List and delete repeated fields?

Asked

Viewed 2,170 times

4

How to compare to lista1 and the lista2, excluding the items repeated in one of them?

  • Put the example of lista1 and lista2?

  • I reversed the edition because it was given an answer assuming what was in the original, in the edited form the answer accepted (and then the others who assumed it) is invalid. I think that until the question was not clear to be answered even, but as it was answered and even accepted, I do not know why, since it does not solve the real problem, then it is better to leave the question as it was and a new question should be asked with the real problem. Just take care to ask a complete and clear question. Even editing does not have everything you would need to answer.

3 answers

8


Assuming T is the type of your lists:

foreach(T t in lista1) 
{
    if(lista2.Contains(t))
        lista2.Remove(t);
}

EDIT:

According to the documentation, there is no need to check if the item exists before removing. You can remove it direct. There will be no error. You can do it like this:

foreach(T t in lista1) 
{
    lista2.Remove(t);
}

Case t does not exist in lista2 nothing will happen.

  • pasta, I’ll test

  • 1

    Cool! I don’t know if you’ve seen the post’s Dit.. Simpler :-)

  • Friend, now that I’ve seen your details about the lists. Do both types inherit from a common class? Product Class, for example? If yes, you should compare through the method Equals(). If they’re the same, then you remove!

  • that’s right, the lists contain different objects, although both have the same attributes

  • @Italorodrigo if they are different objects that won’t work. I actually think the question is unclear on that and probably all the answers would be wrong with this new information. I would need more details to give an answer that really solves the problem, but probably in another question, It is already compromised. If you tell me that this or another answer worked, what you claim here is not true.

  • @bigown I answered when his question consisted only of the first line. I did not have the rest of the body. I commented in that same answer what he could do to make it work.

  • @igorventurelli I think I will reverse the edit to leave the question according to the answers, because in the current form no answer is valid, then it can ask another question to solve the real problem.

  • Thanks guys. I’ll leave this question here and I’ll create another one explaining better the question of my doubt.

  • I have the same situation but @Maniero removed my question, my list is not removed duplicate items in this way that you passed and are of the same type, there is another way ?

  • I don’t know if I got it right, @Standalone , but I think you can use a Set implementation instead of a list.

Show 5 more comments

6

If what you want are the elements of the list 2 that do not exist in the list 1 use the method Except:

var notInList1 = lista2.Except(lista1).ToList();

Example:

lista 1 => 1, 2, 3, 4
lista 2 => 1, 3, 4, 5, 6

notInList1 => 5, 6

5

Browser other questions tagged

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