Using pre-existing iqueryable to filter another iqueryable on Entity Framework

有些话、适合烂在心里 提交于 2019-12-25 03:55:32

问题


I have a many to many relationship (Sites, Categories, CategoriesXSite), I need to get all categories filtering by certain site names so I did my homework and made this linq:

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

it works perfectly, the thing is I already have a method that filters sites and I want to reuse it like this:

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

this is my filter method:

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

Is this possible to accomplish?

Thanks in advance!


回答1:


If your sites have navigation property to categories you can try this:

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



回答2:


If I understand your requirement correctly, you can accomplish what you want by just selecting from your IQueryable object returned from FilterSites, like:

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

I never use the linq query syntax... I assume that will get you what you want... the method syntax would look like:

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


来源:https://stackoverflow.com/questions/5454227/using-pre-existing-iqueryable-to-filter-another-iqueryable-on-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!