Home > Software design >  Compare two list of objects a in C# and retrieve another list with un match records
Compare two list of objects a in C# and retrieve another list with un match records

Time:12-31

I've two list of object I need to compare this two lists and get result with unmatched records.

var oldlist = new List<Employee>{
new Employee(){EmployeeID=123,Name="XXXX",Gender="Male",code="CSE"},
new Employee(){EmployeeID=231,Name="FFF",Gender="Male",code="EC"},
new Employee(){EmployeeID=431,Name="GGG",Gender="Female",code="IT"},
new Employee(){EmployeeID=543,Name="DDD",Gender="Female",code="IT"}
}
var newlist = new List<Employee>{
new Employee(){EmployeeID=123,Name="HHH",Gender="Male",code="IT"},
new Employee(){EmployeeID=231,Name="JJJ",Gender="FeMale",code="CSE"},
new Employee(){EmployeeID=431,Name="GGG",Gender="Female",code="IT"},
new Employee(){EmployeeID=543,Name="DDD",Gender="Female",code="IT"}
}

In above code EmployeeId will be same but there will be change in any other values like for EmployeeId 123 and 231 other values(Name,Gender,code)can changed I need to get only the changed records from newlist in above example result should give only records with employeeId 123 and 231 because remaining records are unchanged.

CodePudding user response:

you can use list1.except(list2) to find the set difference between two lists: -find-the-set-difference-between-two-lists-linq

 var result= oldlist.Except(newlist).ToList();

edit:

You can use the following method to compare two lists of objects:

        List<Employee> list=new List<Employee>();
        foreach (var item in oldlist)
        {
            if(!newlist.Any(p=>p.EmployeeID==item.EmployeeID && p.Name == item.Name && p.Gender == item.Gender && p.code == item.code))
               list.Add(item);
        }

CodePudding user response:

@HosseinSabziani answer is also correct

Alternatively, You can use System.Linq.Enumerable extensions to do this,

Like this

Employee class

   class Employee: IEqualityComparer<Employee>
   {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string code { get; set; }
        public bool Equals(Employee x, Employee y)
        {
            return x.EmployeeID == y.EmployeeID;
        }
    
        public int GetHashCode(Employee obj)
        {
            return obj.EmployeeID.GetHashCode();
        }
   }
var unmatched = oldlist
.Where(old => !newlist.Any(newitem => new Employee().Equals(old, newitem)))
.ToList();

// or

var unmatched = oldlist
.Where(old => !newlist.Any(newitem => newitem.EmployeeID == old.EmployeeID))
.ToList();

Also with Join and Where extension

var unmatched = (from old in oldlist
       join newitem in newlist on 
       old.EmployeeID equals 
       newitem.EmployeeID into joined from j in joined.DefaultIfEmpty()
       here j == null
       select old).ToList();

full source code

// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;
using System.Collections.Generic;
using System.Linq;

public class HelloWorld
{
    class Employee: IEqualityComparer<Employee>
   {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string code { get; set; }
        public bool Equals(Employee x, Employee y)
        {
            return x.EmployeeID == y.EmployeeID;
        }
    
        public int GetHashCode(Employee obj)
        {
            return obj.EmployeeID.GetHashCode();
        }
   }

    public static void Main(string[] args)
    {
        var oldlist = new List<Employee>{
           new Employee(){EmployeeID=123,Name="XXXX",Gender="Male",code="CSE"},
           new Employee(){EmployeeID=231,Name="FFF",Gender="Male",code="EC"},
           new Employee(){EmployeeID=431,Name="GGG",Gender="Female",code="IT"},
           new Employee(){EmployeeID=543,Name="DDD",Gender="Female",code="IT"}
       };
       
       var newlist = new List<Employee>{
          new Employee(){EmployeeID=123,Name="HHH",Gender="Male",code="IT"},
          new Employee(){EmployeeID=231,Name="JJJ",Gender="FeMale",code="CSE"},
          new Employee(){EmployeeID=431,Name="GGG",Gender="Female",code="IT"},
          new Employee(){EmployeeID=543,Name="DDD",Gender="Female",code="IT"}
       };
       
       var unmatched = oldlist
        .Where(old => !newlist.Any(newitem => new Employee().Equals(old, 
        newitem)))
        .ToList();
       
       Console.WriteLine ("Len: "   unmatched.Count);
    }
}
  •  Tags:  
  • c#
  • Related