Question: I have a `List<Organisation>` Organisations.

Each `Organisation` has a property `List<int> Categories`.

I also have a separate `List<int> DisplayCategories`

I want to create a new `List<Organisation>` called `DisplayOrganisations`.

`DisplayOrganisations` will contain `Organisation`'s whos `Categories` appear in my `List<int> DisplayCategories`.

How can I achieve this using linq or lambda?

The following is incorrect:

```
DisplayOrganisations = (from o in Organisations
                        where o.Categories.Intersect(DisplayCategories.CategoryIds) 
                        select o).ToList();

```

I get an error:

> 
> Cannot implicitly convert type
>  'System.Collections.Generic.IEnumerable' to 'bool'
> 
> 
> 

I believe that this is because I am selecting Organisations who's sub list of Categories need to be compared to a separate list DisplayCategories.

Any ideas?

Answer: 
Sure intersect will not give a boolean. you may want to check if there is any elements exist in it use `Any`.

```
DisplayOrganisations = Organisations
                       .Where(o => o.Categories.Intersect(DisplayCategories.CategoryIds)
                                               .Any()).ToList();

```

~~Or better approach.
 DisplayOrganisations = Organisations.Where(o => o.Categories
 .Any(xDisplayCategories.CategoryIds.Contains)).ToList();~~

As you can see in the image Intersect uses hashset which is faster than second approach because Hashset has time complexity of `O(1)` for both adding and removing items. So in general Intersect is `O(n+m)`. but the second approach using `Any` and `Contains` had time complexity of `O(n^2)` which could become much slower.Thanks to [Dmitry Dovgopoly](https://stackoverflow.com/users/1237491/dmitry-dovgopoly).

[![enter image description here](https://i.stack.imgur.com/YFc6l.png)](https://i.stack.imgur.com/YFc6l.png)
If you need only those Organisations where all elements are in `CategoryIds` list you can use mixture of `Except()` and `Any()` methods :

```
var DisplayOrganisations  = Organisations.Where(o => 
                         !o.Categories.Except(DisplayCategories.CategoryIds).Any()).ToList();

```

If you just want to check that there is at least one id in the list of ids you can use `.Any` method :

```
var DisplayOrganisations  = Organisations.Where(o => o.Categories
                                      .Any(DisplayCategories.CategoryIds.Contains)).ToList();

```