简体   繁体   English

使用预先存在的iqueryable过滤实体框架上的另一个iqueryable

[英]Using pre-existing iqueryable to filter another iqueryable on Entity Framework

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: 我有很多对很多的关系(Site,category,categoryXSite),我需要通过某些站点名称来过滤所有类别,因此我完成了作业并做了以下内容:

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! 提前致谢!

如果您的站点具有类别的导航属性,则可以尝试以下操作:

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

If I understand your requirement correctly, you can accomplish what you want by just selecting from your IQueryable object returned from FilterSites, like: 如果我正确理解了您的要求,则可以通过从FilterSites返回的IQueryable对象中进行选择来完成所需的工作,例如:

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: 我从不使用linq查询语法...我想这将为您提供所需的内容...方法语法如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM