So I am trying to find the difference between 2 lists of type Person. This is the person class:
class Person
{
public int Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(int age, string firstName, string lastName)
{
this.Age = age;
this.FirstName = firstName;
this.LastName = lastName;
}
}
and in my code, I create 2 variables, List<Person> list1 and List<Person> list2.
I fill list1 with the following variables:
list1.Add(new Person(20, "first1", "last1"));
list1.Add(new Person(30, "first2", "last2"));
list1.Add(new Person(40, "first3", "last3"));
list1.Add(new Person(50, "first4", "last4"));
and fill list2 with the following:
list2.Add(new Person(30, "first2", "last2"));
list2.Add(new Person(50, "first4", "last4"));
My goal is to have another list (List<Person> list3) with list1[0] and list[2] since that is what is not included in list2. I tried using list3 = list1.Except(list2).ToList(); but that just returns a copy of list1 and if i do list3 = list2.Except(list1).ToList(); it returns a copy of list2. How do I solve this? Am I using Except() right?