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

  1. Pingback: Twitter / willcode4coffee: First blog post in almost s...

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>