Querying Generic Content Types
I'm writing a widget that queries for specific pieces of generic content. I'm trying to implement a search based on a keyword. Here is my code:
ContentManager manager = ContentManager.GetManager();
IQueryable<
ContentItem
> allItems = manager.GetContent()
.Where(n => n.Status == ContentLifecycleStatus.Live)
.Where(n => n.Content.ToLower().Contains(searchTerm.ToLower()))
;
Hello Chris,
Could you try the following
1. Check what
manager.GetContent()
.Where(n => n.Status == ContentLifecycleStatus.Live)
searchTerm.ToLower()
.Where(n => n.Content.ToLower().Contains("test"))
Hi Ivan,
#1 returns all 6 items in the generic content exactly as you would expect.
#2 is throwing an error :Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
If I remove the ToLower portion it just returns an empty set. The ToLower is obviously important for case insensitive searching.
It seems if I remove the "ToLower" method and hardcode the searchterm it works OK. However, both of these are going to be necessary functionality. I need to be able to pass a searchterm into the query (I'm making sure to case as a string before using) and I need to be able to perform case insensitive searches. Any ideas?
Hi Chris,
"it is possible that there is an issue with the parser and if this is the case you can expose a local variable for searchTerm.ToLower()
"
Does this help? Remove the ToLower from the query expression.
Regards,
Ivan Dimitrov
the Telerik team
Yes, I tried that. It works ONLY if I hardcode the searchTerm variable. Here is my code:
string searchTerm = this.keywords.Text.ToString().Trim();
ContentManager manager = ContentManager.GetManager();
IQueryable<
ContentItem
> allItems = manager.GetContent()
.Where(n => n.Status == ContentLifecycleStatus.Live)
.Where(n => n.Content.Contains(searchTerm));
string searchTerm = this.keywords.Text.ToString().Trim();
ContentManager manager = ContentManager.GetManager();
IQueryable<
ContentItem
> allItems = manager.GetContent()
.Where(n => n.Status == ContentLifecycleStatus.Live)
.Where(n => n.Content.Contains("test"));
Hi Chris,
2. Check whether there will be results if you use a string like "test" in the expression below, because it is possible that there is an issue with the parser and if this is the case you can expose a local variable for searchTerm.ToLower()
.Where(n => n.Content.ToLower().Contains("test"))
There is some issue with the OQL and we will need some more time to see how the issue can be fixed.
All the best,
Ivan Dimitrov
the Telerik team