i have a list of project objects:
IEnumerable<Project> projects
a Project class as a property called Tags. this is a int[]
i have a variable called filteredTags which is also a int[].
So lets say my filtered tags variable looks like this:
int[] filteredTags = new int[]{1, 3};
I want to filter my list (projects) to only return projects that have ALL of the tags listed in the filter (in this case at least tag 1 AND tag 3 in the Tags property).
I was trying to use Where() and Contains() but that only seems to work if i am comparing against a single value. How would i do this to compare a list against another list where i need a match on all the items in the filtered list ??
EDIT: better yet, do it like that:
EDIT2: Honestly, I don’t know which one is better, so if performance is not critical, choose the one you think is more readable. If it is, you’ll have to benchmark it somehow.
Probably
Intersectis the way to go:Although that seems a little ugly at first. You may first apply
DistincttofilteredTagsif you aren’t sure whether they are all unique in the list, otherwise the counts comparison won’t work as expected.