Query nodes based on selected Checkbox List Values
We're building out a news blog using a checkbox list of pre-defined categories and want to be able to find nodes based on their selected checkbox values. I can't find an up to date method for doing this in Umbraco 8
Any pointers would be helpful... I'm sure other's are doing this already.
I presume you want this to automatically pick up new categories as you add them.
If so examine is probably the easiest. Add a SelectedCategory field to the examine index and on document publish, add all the selected boxes to that field.
You can then do an examinequery on that field only with all selected values seperated by a space (best to change spaces in category names to _ or something similiar when building the field as the default examine behaviour sees a space as a new term)
If the categories will not change, since its just a bool. You can do a simple nucache node query
[untested code]
var filteredNodes = NewsArea.Children<NewsItem>();
if(checkbox1 || checkbox2 || checkbox3 || checkbox4){
// at least one is checked
filteredNodes = filteredNodes.Where(n =>
(checkbox1 == false || n.Category1) &&
(checkbox2 == false || n.Category2) &&
(checkbox3 == false || n.Category3) &&
(checkbox4 == false || n.Category4) &&
)
}
I'm looking for something more simplified like a tag query.
I had a similar query setup using Tags where the tag on these pages was setup as Umbraco.TagQuery.GetContentByTag("newsItem") my client wanted to eliminate content editors errors in Tagging, but I don't see an easy way to do the same type of query on a Checkbox List...
'object' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains<string>(IQueryable<string>, string)' requires a receiver of type 'IQueryable<string>'
Query nodes based on selected Checkbox List Values
We're building out a news blog using a checkbox list of pre-defined categories and want to be able to find nodes based on their selected checkbox values. I can't find an up to date method for doing this in Umbraco 8
Any pointers would be helpful... I'm sure other's are doing this already.
I presume you want this to automatically pick up new categories as you add them.
If so examine is probably the easiest. Add a SelectedCategory field to the examine index and on document publish, add all the selected boxes to that field.
You can then do an examinequery on that field only with all selected values seperated by a space (best to change spaces in category names to _ or something similiar when building the field as the default examine behaviour sees a space as a new term)
If the categories will not change, since its just a bool. You can do a simple nucache node query
[untested code]
I'm looking for something more simplified like a tag query.
I had a similar query setup using Tags where the tag on these pages was setup as Umbraco.TagQuery.GetContentByTag("newsItem") my client wanted to eliminate content editors errors in Tagging, but I don't see an easy way to do the same type of query on a Checkbox List...
Is there is a way to do is using some type of .Where(x => x.Value("newsCategories").Contains(queryCategory)
When I try this
I get the following error:
Does it need to be converted to a string? Something like this?
No... while I can convert the element to a string on the node itself during an individual page view.
String.Join(", ", Model.NewsCategories.ToList()
This causes an error when it's in a list of nodes on the parent document.
Compiler Error Message: CS0266: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable
There is no direct ToString() function available on these IEnumerable lists as far as I can tell.
This isn't really an answer, but I feel like its somehow related to this question I posted last week: https://our.umbraco.com/forum/using-umbraco-and-getting-started/107033-generate-list-from-ienumberable
I had to rush the site out the door so hid that section for now but will post back if I find a solution.
is working on a reply...