Click here to Skip to main content
15,667,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have and Employee class

public class Employee
        public int ID { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public string Address { get; set; }
        public string ContactNo { get; set; }

Fill method
private static void FillEmployeeList(ref List<Employee> lt1, ref List<Employee> lt2)
            lt1 = new List<Employee> {new Employee{ID=1,Name="Kavya",Age="24",Address="No.1,Nehru Street,Chennai",ContactNo="9874521456"},
            new Employee{ID=2,Name="Ravi",Age="24",Address="Flat No.25/A1,Gandhi Street,Chennai",ContactNo="9658745258"},
            new Employee{ID=3,Name="Lavnya",Age="30",Address="No.12,Shastri nagar,Chennai",ContactNo="5214587896"},
            new Employee{ID=4,Name="Rupa",Age="31",Address="No.23/5,Nehru Street,Chennai",ContactNo="9874521256"},
            new Employee{ID=5,Name="Divya",Age="32",Address="No.1/227,Nehru Street,Chennai",ContactNo="8541256387"},            

    lt2 = new List<Employee> {new Employee{ID=1,Name="Kavya",Age="24",Address="No.1,Nehru Street,Chennai",ContactNo="9874521456"},
    new Employee{ID=2,Name="Ravindran",Age="30",Address="Flat No.25/A1,Gandhi Street,Chennai",ContactNo="9658745258"},
    new Employee{ID=3,Name="Chandru",Age="30",Address="No.12,Shastri nagar,Chennai",ContactNo="5214587896"},
    new Employee{ID=4,Name="Rakesh",Age="32",Address="No.23/5,Nehru Street,Chennai",ContactNo="9874021256"},
    new Employee{ID=5,Name="Suresh",Age="32",Address="No.1/227,Nehru Street,Chennai",ContactNo="8541056387"},
    new Employee{ID=11,Name="Suryakala",Age="28",Address="No.1,Pillayar koil Street,Chennai",ContactNo="9541204782"},
    new Employee{ID=12,Name="Thivya",Age="41",Address="No.42,Ellaiamman koil Street,Chennai",ContactNo="9632140874"},

Comparing two list,

protected List<Employee> ListCompare(List<Employee> lt1, List<Employee> lt2)
            FillEmployeeList(ref lt1, ref lt2);
            List<Employee> lst = new List<Employee>();

            if (lt1.Count > 0 && lt2.Count > 0)
                // Displaying Matching Records from List1 and List2 by ID

                var result = (from l1 in lt1
                              join l2 in lt2
                              on l1.ID equals l2.ID
                              orderby l1.ID
                              select new

                                  ID = l1.ID,
                                  Name = (l1.Name == l2.Name) ? "$" : (l2.Name + " (Modified)"),
                                  Age = (l1.Age == l2.Age) ? "$" : (l2.Age + " (Modified)"),
                                  Address = (l1.Address == l2.Address) ? "$" : (l2.Address + " (Modified)"),
                                  ContactNo = (l1.ContactNo == l2.ContactNo) ? "$" : (l2.ContactNo + " (Modified)")

                // Displaying Records from List1 which is not in List2
                var result1 = from l1 in lt1
                              where !(from l2 in lt2
                                      select l2.ID).Contains(l1.ID)
                              orderby l1.ID
                              select new
                                  ID = l1.ID,
                                  Name = " Deleted",
                                  Age = " Deleted",
                                  Address = " Deleted",
                                  ContactNo = " Deleted"

                // Displaying Records from List1 which is not in List2
                var result2 = from l1 in lt2
                              where !(from l2 in lt1
                                      select l2.ID).Contains(l1.ID)
                              orderby l1.ID
                              select new
                                  ID = l1.ID,
                                  Name = l1.Name + " (Added)",
                                  Age = l1.Age + " (Added)",
                                  Address = l1.Address + " (Added)",
                                  ContactNo = l1.ContactNo + " (Added)"

                var res1 = result.Concat(result1).Concat(result2);

                foreach (var item in res1)
                    Employee emp = new Employee();
                    //Response.Write(item + "<br/>");
                    emp.ID = item.ID;
                    emp.Name = item.Name;
                    emp.Age = item.Age;
                    emp.Address = item.Address;
                    emp.ContactNo = item.ContactNo;
            return lst;

I want the above ListCompare in generalize to use any king of objects not only for Employee, for other class also(Student, Person etc.). Pls can anyone help me?

Displaying Result,

<pre lang="c#">
List<employee> lt1 = new List<employee>();
List<employee> lt2 = new List<employee>();
List<employee> resultset = new List<employee>();
//string value = "ID";
StringBuilder htmlTable = new StringBuilder();
resultset = ListCompare(lt1, lt2);
foreach(var item in resultset)
" + item.ID + "" + item.Name + "" + item.Age + "" + item.Address + "" + item.ContactNo + "
PlaceHolder1.Controls.Add(new Literal { Text = htmlTable.ToString() });

ID Name Age Address ContactNo
1 $ $ $ $
2 Ravindran (Modified) 30 (Modified) $ $
3 Chandru (Modified) 30 (Modified) $ $
4 Rakesh (Modified) 32 (Modified) $ 9874021256(Modified)
5 Suresh (Modified) $ $ 8541056387(Modified)
11 Suryakala (Added) 28 (Added) No.1,Pillayar
koil Street,
Chennai (Added) 9541204782 (Added)
12 Thivya (Added) 41 (Added) No.42,Ellaiamman koil
Street,Chennai(Added) 9632140874 (Added)

What I have tried:

Getting correct output but i want in generalized way to use it for any class of objects like Student, Person etc.
Updated 13-Jul-16 0:55am

Pretty much, you can't generalise it in the way you want - if you want to compare by fields which are specific to the Student or Person class.
You can do it if you create a base class and derive all your other classes from it:
public class Employee
    public int ID { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
    public string Address { get; set; }
    public string ContactNo { get; set; }
    public List<T> ListCompare<T>(List<T> lt1, List<T> lt2) where T:Employee
        List<T> lst = new List<T>();
        return lst;

You can even make Employee abstract so you can't accidently create an instance of it.
Share this answer
Poongodi V 12-Jul-16 4:47am    
Thank you.
OriginalGriff 12-Jul-16 4:55am    
You're welcome!
"I want the above ListCompare in generalize to use any king of objects"
That, as OriginalGriff advises you here, is difficult; to do this would require complex use of reflection, and, I think you'd have to some way (Custom Attributes ?) to add meta-data to the class instances you compared to make the lists of fields, properties, etc. that reflection gave you access to "meaningful."

... edit ...

There is a four part series by Ehsan Sajjad on equality in .NET here on CodeProject: [^].

... end edit ..

There is an open-source project on GitHub [^], "Compare-Net-Objects," being very actively developed by Greg Finzer and others, that takes on this difficult task, and which, in my experience, is quite robust. You can install this project using NuGet: [^].

The current feature-set of Compare-Net-Objects:

* Compare Children (on by default)
* Handling for Trees with Children Pointing To Parents (Circular References)
* Compare Primitive Types
* Compare Structs
* Compare IList Objects
* Compare Single and Multi-Dimensional Arrays
* Compare IDictionary Objects
* Compare Publicly visible Class Fields and Properties
* Compare Private Fields and Properties (off by default)
* Compare Enums
* Compare Timespans
* Compare Guids
* Compare Classes that Implement IList with Indexers
* Compare DataSet Data
* Compare DataTable Data
* Compare DataRow Data
* Compare LinearGradient
* Compare HashSet
* Compare URI
* Compare IPEndPoint
* Compare Types of Type (RuntimeType)
* Source code in C#
* NUnit Test Project Included
* Ability to load settings from a config file for use with powershell
* Several configuration options for comparing private elements, ignoring specific elements, including specific elements.
* Property and Field Info reflection caching for increased performance
* Rich Differences List or simple DifferencesString
* Supports custom comparison functions
* ElapsedMilliseconds indicates how long the comparison took
* Ability to IgnoreCollectionOrder
* Thread Safe
* Ability to pass in the configuration
* Ability to ignore indexer comparison
* Ability to ignore types
* Interface member filtering
* Ability to treat string.empty and null as equal
* Beyond Compare Report
* WinMerge Report
* CSV Report
* User Friendly Repo
Share this answer
Poongodi V 15-Jul-16 3:34am    
thank you. Any sample coding available to compare two list using Compare .NET objects. If so pls share.
Please refer the below link for the solution
c# - compare two list of objects using linq dynamically - Stack Overflow[^]
Share this answer
BillWoodruff 13-Jul-16 13:06pm    
The above link leads to a solution that compares two lists of objects with the same structures (i.e. the objects are instances of the same Class, Struct, Type) using Linq. That is easy, but you asked about possibly comparing lists of objects that may have different internal structures: that's complex !
Poongodi V 14-Jul-16 8:57am    
yes. I want the way you said but still I am looking for the solution. Pls share if you get any idea.

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