开发者

Iqueryable as a subquery to filter another iqueryable

I have a many to many relationship (Sites, Categories, CategoriesXSite) and two iqueryable defined v开发者_开发问答ariables 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 categories iqueryable based on sites iqueryable so that way I can have any categories with any filters plus another filter of the categories that has sites containing certain filter, some thing like this:

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

I've made a similar question before when I didn't need to filter categories as well (here)

Thanks a lot,


You'll probably want either

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜