Iqueryable as a subquery to filter another iqueryable

I have many, many relationships (Sites, Categories, XSite Categories) and two iqueryable specific variables like this:

IQueryable<Site> sitesQuery = from s in db.Sites
                         where s.Name.Contains(siteWord)
                         select s



IQueryable<SiteCategorie> categoriesQuery = from c in db.SiteCategories
                                       where c.Parent.ID == 1
                                       select c;

I want to be able to apply a filter to iqueryable categories based on iqueryable sites so that I can have any categories with any filters and another category filter with sites containing a specific filter, something like this:

from c in categoriesQuery 
where c.Sites == sitesQuery
select c

I asked a similar question before when I didn't need to filter categories ( here )

Many thanks,

+3
source share
1 answer

You'll probably need

from c in categoriesQuery  
where c.Sites.Any(s => sitesQuery.Contains(s))
select c 

or

from c in categoriesQuery  
where c.Sites.All(s => sitesQuery.Contains(s))
select c 

depending on your use case.

+6
source

All Articles