Here is how to get list of records that do not exists in another list using linq.
Let’s say we have below two list.
class LinqTwoListProgram { static void Main(string[] args) { List<Employee> employee1 = new List<Employee>(); employee1.Add(new Employee() { EmpId = 101 }); employee1.Add(new Employee() { EmpId = 201 }); employee1.Add(new Employee() { EmpId = 301 }); List<Employee> employee2 = new List<Employee>(); employee2.Add(new Employee() { ID = 101 }); employee2.Add(new Employee() { ID = 201 }); employee2.Add(new Employee() { ID = 301 }); employee2.Add(new Employee() { ID = 401 }); } } class Employee { public int EmpId { get; set; } }
So linq query would be as below,
var distinctRecordsFromList2 = employee2.Where(e2 => !employee1.Any(e1 => e2.EmpId == e.EmpId));