jdweng's answer is generally fine but there are some options depending on what object you list is made of;
If you are using classes, then the following pattern is just fine ...
List<Test> list1 = new List<Test>() {
new Test() { Name = "Foo" },
new Test() { Name = "Bar" },
new Test() { Name = "OnlyInList1" },
};
List<Test> list2 = new List<Test>() {
new Test() { Name = "Foo" },
new Test() { Name = "Bar" },
new Test() { Name = "OnlyInList2" },
};
list1.Where(l1 =>
list2.Any(l2 => l1.Property == l2.Property));
However, if you are using a primitive type, such as ints, then you may find Intersect() is the best option, as it's a little bit faster when using primitives.
List<int> list1 = new List<int>() { 1, 2, 3, 4, 5 };
List<int> list2 = new List<int>() { 1, 2, 3, 4, 6 };
list1.Intersect(list2);
Why you wouldn't use Intersect on complex objects ...
The reason Intersect may not be the best option for classes would be that you must do a Select() on the comparison list to get the property values you wish to compare, like so ...
List<Test> list1 = new List<Test>() {
new Test() { Name = "Foo" },
new Test() { Name = "Bar" },
new Test() { Name = "OnlyInList1" },
};
List<Test> list2 = new List<Test>() {
new Test() { Name = "Foo" },
new Test() { Name = "Bar" },
new Test() { Name = "OnlyInList2" },
};
var list2Names = list2.Select(e => e.Name);
list1.IntersectBy(x, e => e.Name);
Personally, I like the syntax of IntersectBy but it's slower in comparison to the Where(e => f.Any()); on complex objects.
So, in summary, the options above cover must uses but in your case, the Where pattern jdweng mentioned is suitable.
.net Fiddle of the cases;
Results:
// for 100,000 operations ...
// TestWithClass
// Intersect : 83ms
// Where.Any : 66ms
// TestWithPrimitive
// Intersect : 45ms
// Where.Any : 54ms