Finding Common Values in Two Lists in C# Using LINQ

Finding common values in two lists is simple in .NET 3.5 thanks to LINQ!  I found this example here and here.

In practice here is what I did:

var currentOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(this.OperatorId) select fac.ID; var deleteOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(operatorId) select fac.ID; var commonFacilityIds = currentOperatorFacilityIds.Intersect(deleteOperatorFacilityIds);

Really simple!  I love LINQ.

One thought on “Finding Common Values in Two Lists in C# Using LINQ”

Leave a Reply

Your email address will not be published. Required fields are marked *