使用预先存在的 iqueryable 来过滤实体框架上的另一个 iqueryable
我有一个多对多的关系(站点、类别、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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的网站具有类别导航属性,您可以尝试以下操作:
If your sites have navigation property to categories you can try this:
如果我正确理解您的要求,您只需从 FilterSites 返回的 IQueryable 对象中进行选择即可完成您想要的操作,例如:
我从不使用 linq 查询语法...我认为这会得到您想要的...该方法语法如下:
If I understand your requirement correctly, you can accomplish what you want by just selecting from your IQueryable object returned from FilterSites, like:
I never use the linq query syntax... I assume that will get you what you want... the method syntax would look like: