问题描述
我有很多与许多关系(网站,类别,类别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");
问题描述
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");