Take the 2-minute tour ×

i have an object called Person. It has properties First, Last, Age, etc . . .

I have two arrays of Person objects.

I want to have some function to take two arrays

Person[] firstlist =  . .
Person[] secondList =  . .

and have it spit out two new arrays

Person[] peopleinFirstListandNotSecond
Person[] peopleinSecondListandNotFirst

Since these are not string arrays, i would want to do a compare on first and last name and age to determine if its the same person

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Here is a linq function (IEnumerable<T>.Except(...)) that will do what you need.


share|improve this answer
That is actually awesome. –  Alex Bagnolini Dec 15 '09 at 13:24
@Alex - LINQ is awesome. –  Daniel A. White Dec 15 '09 at 13:28
You should probably add that he would need to compare the objects for equality, either by implementing a comparer, or doing the comparison within the lambda. Except on its own will not solve the problem. –  Winston Smith Dec 15 '09 at 13:29
The link is to the one with the IEqualityComparer. –  Daniel A. White Dec 15 '09 at 13:31
add comment

You could write a comparer (implement the IEqualityComparer interface) then use it with the Except extension method, as other posters have noted.

Or, you could just do the comparison within the lambda eg

var peopleinFirstListAndNotSecond =     
    Where( p => 
            !secondList.Any( s => 
                s.Age == p.Age && 
                s.FirstName == p.FirstName && 
                s.SecondName == p.SecondName
share|improve this answer
add comment

Implement IComparable (see SO: IComparable and Equals) and then loop through each list, building the required two output lists.

share|improve this answer
Linq will do this for you. –  Daniel A. White Dec 15 '09 at 13:31
add comment

Your Answer


