Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I am trying to compare the contents of 2 lists and spit out any items in the list not found in both. I am not sure what the best method for this is, i have tried this:

C#
List<object1> list = GetList();
List<object1> list2 = list;

            foreach(list g in list)
            {
                if (!list2.Contains(g))
                {
                    Method1();
                }
            }


Method1 is fires for each item in the list where it shouldnt, do i need a better comparer? object1 is an object with multiple fields.

Regards
Carl
Posted

Example :
ListA : A,B,C
ListB : A,B,E

=> compare A with B :

List<string> result = ListA.Except(ListB).ToList();

=> result : C

You can read more at this link :

http://stackoverflow.com/questions/12795882/quickest-way-to-compare-two-list[^]
 
Share this answer
 
Unclear what is your exact requirement, but you can try with linq like below
C#
if(!list.Any(g=>list2.Contains(g))
{
   Method1();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900