find common items across multiple lists in C#

Solution 1:

Assuming you use a version of .Net that has LINQ, you can use the Intersect extension method:

var CommonList = TestList1.Intersect(TestList2)

Solution 2:

If you have lists of objects and want to get the common objects for some property then use;

var commons = TestList1.Select(s1 => s1.SomeProperty).ToList().Intersect(TestList2.Select(s2 => s2.SomeProperty).ToList()).ToList();

Note: SomeProperty refers to some criteria you want to implement.

Solution 3:

Assuming you have LINQ available. I don't know if it's the fastest, but a clean way would be something like:

var distinctStrings = TestList1.Union(TestList2).Distinct();

var distinctStrings = TestList1.Union(TestList2);

Update: well never mind my answer, I've just learnt about Intersect as well!

According to an update in the comments, Unions apply a distinct, which makes sense now that I think about it.