TechBubbles Microsoft Technology BLOG

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 :

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud