score:2
Accepted answer
You can pair up each item with category IDs, like this:
var items = _umbracoHelper.GetPage(ItemsPage.ModelTypeAlias).Children
.Select(c => new {
Child = c
, CategoryIds = c
.GetPropertyValue<IEnumerable<IPublishedContent>>(UmbracoAlias.Item.Categories)
.Select(y => y.Id)
.ToList()
})
.Where(x => level1Category == 0 || x.CategoryIds.Contains(level1Category))
.Where(x => !level2Categories.Any() || x.CategoryIds.Intersect(level2Categories.AsEnumerable()).Any())
.Where(x => !level3Categories.Any() || x.CategoryIds.Intersect(level3Categories.AsEnumerable()).Any())
.Select(x => x.Child);
This does the filtering on children paired up with their category IDs, and then keeps only the Child
object in the final projection.
You could further simplify this by combining all three Where
clauses:
var items = _umbracoHelper.GetPage(ItemsPage.ModelTypeAlias).Children
.Where(c => {
var categoryIds = c
.GetPropertyValue<IEnumerable<IPublishedContent>>(UmbracoAlias.Item.Categories)
.Select(y => y.Id)
.ToList();
if (level1Category != 0 && !categoryIds.Contains(level1Category)) {
return false;
}
if (level2Categories.Any() && !categoryIds.Intersect(level2Categories.AsEnumerable()).Any()) {
return false;
}
if (level3Categories.Any() && !categoryIds.Intersect(level3Categories.AsEnumerable()).Any()) {
return false;
}
return true;
});
Source: stackoverflow.com
Related Articles
- Use same, looked-up value across multiple linq Where clauses without looking up more than once
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value
- Multiple WHERE Clauses with LINQ extension methods
- Joining multiple where clauses in LINQ as OR instead of AND
- Linq to SQL multiple conditional where clauses
- Entity Framework Core LINQ query with multiple where clauses
- Chain together multiple complex WHERE clauses in LINQ to SQL
- Linq query and multiple where clauses with OR only first condition gets evaluated
- Linq filtering results with multiple Where clauses
- Use LINQ to concatenate multiple rows list with same value into single row
- C# Linq XML Query where multiple elements of same name exist
- Using LINQ with a WHERE to ask for multiple possible results without redundant "||" commands
- Linq to Datatable with multiple where clauses
- LINQ LEFT JOIN, Combine multiple WHERE clauses with OR
- C# Linq to select multiple columns by group and apply sum(aggregate function) on value field at the same time
- How to Create a Linq Query with Multiple Conditional Where Clauses
- Remove multiple keys with same value data from dictionary in C# using Linq
- How to process multiple where clauses in a LINQ statement?
- c# linq question about multiple where clauses
- multiple where in linq based on parameters value
- Multiple Include and Where Clauses Linq
- Linq statements, multiple where clauses
- LINQ query with multiple conditional where clauses
- C# Linq XML Query where multiple elements of same name already exists
- LINQ XML Get value of element from multiple where statement
- How to handle null values in LINQ with multiple where clauses
- Left Outer join LINQ query with four entities in CRM 2011 and with multiple where clauses
- Multiple condition on same column inside Linq where
- how to generate where condition without multiple if condition using linq query
- Linq multiple where clauses doesn't work
- Get the number of hours between time Period
- .Where in LinQ not working correctly
- How to get nearest date?
- Is there a way to search two different collection and fill another collection using LINQ?
- How do I use a WPF TreeView HierarchicalDataTemplate with LINQ to Entities?
- Pivoting data from different tables
- Using Linq to Filter a List of Objects based on a Condition
- How can i cast IQueryable<> query to IQueryable<obj.getType()>?
- C# MongoDb driver include child object
- How to simulate .Substring() method in Linq-to-Entities
- linq store select clause as function variable
- linq group by, order by
- Case sensitive varchar match with LINQ
- asp.net mvc adding list to viewmodel
- Passing whole function as lambda expression to LINQ
- ASP. NET MVC3 - How to display all Comments in Post
- how to insert the database multiple gridview choises
- intersection of two list add to another list in mvc
- LINQ query for getting items that match all items of an array of IDs
- Using Max, Group By, Join and Where in Lambda Expression