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

查看:87
本文介绍了使用预先存在的iqueryable来过滤实体框架上的另一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多关系(Sites,Categories,CategoriesXSite),我需要通过某些网站名称来获取所有类别的过滤,所以我做了我的家庭作业,并做了这个linq:

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; 

这是我的过滤器方法:

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

这是否可以完成?

提前感谢

推荐答案

如果您的网站具有导航属性,可以尝试以下几点:

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

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

这篇关于使用预先存在的iqueryable来过滤实体框架上的另一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆