Click here to Skip to main content
15,041,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I would like to know what is the efficient way to find the differences between two list?

Let's say I have a Employee Class.
C#
public class Employee
{
    public int EmployeeID { get; set; }
    public int DepartmentID { get; set; }
    public DateTime StartDate { get; set; }
    public int SupervisorID { get; set; }
    public string FullName { get; set; }        
    public DateTime EndDate { get; set; }
    public string Title { get; set; }
    public string Address { get; set; }
}

I have two lists
C#
List<Employee> empInDB1 = new List<Employee>();
List<Employee> empInDB2 = new List<Employee>();

Something loops through the rows/each employee in the list match the employee from EmployeeID and compare each field and if it is changed and add it to a new list?
C#
var changedData = empInDB1.Intersect(empInDB2);
But I am stuck can someone lead me to the solution. Thanks in advance.
Posted

Try
C#
var changedData = empInDB1.Except(empInDB2);
   
Comments
wonder-FOOL 27-Mar-13 4:38am
   
Seems like I should use Except method but can you explain me how this method matches the EmployeeID's? Shouldn't I add something like .Select(x => x.EmployeeID) to it? Thank you very much for the answer btw.
OriginalGriff 27-Mar-13 4:54am
   
Have a look at MSDN: Except has an override which takes a IEqualityComparer as the second parameter to do exactly what you want.

http://msdn.microsoft.com/en-us/library/bb336390.aspx
wonder-FOOL 27-Mar-13 5:04am
   
Thank you very much. Now I have a better understanding.
OriginalGriff 27-Mar-13 5:50am
   
You're welcome!
Have a look on similar discussion here[^]
   
I would use the Except[^] method (see the code samples).
   
Comments
Zoltán Zörgő 27-Mar-13 4:28am
   
Good point. But I would suggest this overload, because the OP will need an EqualityComparer for this custom class.
wonder-FOOL 27-Mar-13 5:04am
   
Thank you that helped a lot and you are right about the overload.

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