I have the following list of list of Person objects
List<List<Person>> setofPersons=new List<Lits<Person>>();
I add items to it like this.
List<Person> firstPersonList=new List<Person>();
Person p1=new Person(100,"James");
Person p2=new Person(200,"Smith");
firstPersonList.Add(p1);
firstPersonList.Add(p2);
setOfPersons.Add(firstPersonList);
List<Person> secondPersonList=new List<Person>();
Person p3=new Person(100,"James");
Person p4=new Person(200,"Smith");
Person p5=new Person(300,"Thomas");
secondPersonList.Add(p3);
secondPersonList.Add(p4);
secondPersonList.Add(p5);
setOfPersons.Add(secondPersonList);
List<Person> thirdPersonList=new List<Person>();
Person p6=new Person(100,"James");
Person p7=new Person(400,"Amy");
thirdPersonList.Add(p6);
thirdPersonList.Add(p7);
setOfPersons.Add(thirdPersonList);
Now I want to find out the common objects among the lists contained in setOfPersons. This comprison should be done by person Id eg : 100,200,300 etc;
I have tried the following.
List<Person> commonPersons = setOfPersons
.Where(i => setOfPersons.Skip(1).All(x => x.Contains(i)))
.ToList();
But the problem is that count in commomPersons is always 0;
Can someone help me to fix this ?
EDIT I have written the following method in Person class
public int CompareTo(object obj)
{
Person p = (Person)obj;
if (p.personId == personId)
return 1;
else
return 0;
}
Then I changed my LINQ query to the following
List<Person> commonPersons = setofPersons[0].Where(
i => setofPersons.Skip(1).All(x => i.CompareTo(x))
).ToList();
It now generates a compile time error saying " cannot convert bool to int ". I'm new to LINQ. Can someone point out where I have gone wrong ?