使用预先存在的iQueryable来过滤另一个在实体框架上[英] Using pre-existing iqueryable to filter another iqueryable on Entity Framework

本文是小编为大家收集整理的关于使用预先存在的iQueryable来过滤另一个在实体框架上的处理方法,想解了使用预先存在的iQueryable来过滤另一个在实体框架上的问题怎么解决?使用预先存在的iQueryable来过滤另一个在实体框架上问题的解决办法?使用预先存在的iQueryable来过滤另一个在实体框架上问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我有很多与许多关系(网站,类别,类别Xite),我需要通过某些站点名称进行所有类别过滤,因此我做了我的作业并制作了此linq:

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

它运作完美,事情是我已经有了一种过滤站点的方法,我想这样重复使用它:

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

这是我的过滤方法:

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

这可以完成吗?

预先感谢!

推荐答案

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

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

其他推荐答案

如果我正确理解您的要求,则可以通过从过滤器中返回的可观对象选择来完成您想要的工作,例如:

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

我从不使用linq查询语法...我认为这会给您带来想要的东西...方法语法看起来像:

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

本文地址:https://www.itbaoku.cn/post/684932.html

问题描述

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!

推荐答案

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

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:

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");
查看更多