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

发布于 2024-10-26 13:46:14 字数 746 浏览 1 评论 0原文

我有一个多对多的关系(站点、类别、CategoriesXSite),我需要按某些站点名称过滤所有类别,所以我做了功课并制作了这个 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);
}

这可以实现吗?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅 2024-11-02 13:46:14

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

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

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

var filteredcategories = FilterSites(siteWord).SelectMany(s => s.Categories);
顾铮苏瑾 2024-11-02 13:46:14

如果我正确理解您的要求,您只需从 FilterSites 返回的 IQueryable 对象中进行选择即可完成您想要的操作,例如:

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

我从不使用 linq 查询语法...我认为这会得到您想要的...该方法语法如下:

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文