Techbubbles

Comparing Two Generic Lists in C#

Today I come across the problem while comparing two different lists. The basic requirement is finding the records which are present in one list but not in other and vise versa. I thought of sharing this piece of code which might  help for you to do the same task.

Let us take the two Generic lists named timeEntryList and caseDataList. The aim is comparing these two lists and bring the results into two separate list.

   1: var auditTimeEntryList = 

   2: timeEntryList.Where(timeEntry => !caseDataList.Any(caseData => 

   3: caseData.OperatingRoom.Name.Equals(timeEntry.OperatingRoom.Name)

   4: & caseData.StartDate.ToShortDateString().Equals

   5: (timeEntry.ShiftDate.ToShortDateString())));

Now auditTimeEntryList contains the records which are not present in the caseDataList.

Let us write the code for bringing the records which are present in caseDataList but not in timeEntryList.

   1: var auditCaseDataList = caseDataList.Where(caseData => 

   2: !timeEntryList.Any(timeEntry => 

3: caseData.OperatingRoom.Area.AreaId ==

timeEntry.OperatingRoom.Area.AreaId

4: && caseData.StartDate.ToShortDateString() ==

timeEntry.ShiftDate.ToShortDateString()));

Share this post :

Leave a Reply

avatar
  Subscribe  
Notify of