Using a pre-existing iqueryable to filter another iqueryable in the Entity Framework

I have many different relationships (Sites, Categories, XSite Categories), I need all categories to be filtered by specific site names, so I did my homework and did this linq:

var filteredcategories = from c in context.Categories
                       from s in c.Sites
                       where s.Name.Contains(siteWord)
                       select c;   

It works fine, the fact is that I already have a method that filters sites, and I want to reuse it as follows:

var filteredcategories = from c in context.Categories
                       where c. Sites == FilterSites(siteWord)
                       select c; 

this is my filtering method:

public IQueryable<Site> FilterSites(string word)
{
      return (from s in context.Sites
              where s.Name.Contains(word)
              select s);
}

Is it possible to do this?

Thanks in advance!

0
source share
2 answers

If your sites have category navigation property, you can try the following:

var filteredcategories = FilterSites(siteWord).SelectMany(s => s.Categories);
+1
source

, , , IQueryable, FilterSites, :

var filteredcategories = from c in FilterSites(siteWord) select c; 

linq... , , ... :

var filteredcategories = FilterSites(siteWord).Where(s => s.Something = "something");
0

All Articles