LINQ How to return common objects across multiple lists

Viewed 57

This may be a silly question but I cannot find the solution for more than 2 lists. Say I have the following lists:

List<BusinessProcess> List1 = new List<BusinessProcess>(){ obj1, obj2, obj3, obj4 };
List<BusinessProcess> List2 = new List<BusinessProcess>(){ obj2, obj3 };
List<BusinessProcess> List3 = new List<BusinessProcess>(){ obj3, obj4 };

I want to return the objects that are equal among all 3 lists (obj3 in this case). How would I do this using LINQ?

1 Answers

Based on last comments, the solution is :

Filter all common BusinnesProcess, based on obj.id

List<BusinessProcess> result = List1
.Where(y => List2.Select(x => x.id).Intersect(List3.Select(x => x.id))
    .Contains(y.id))
.ToList();
Related