Querying Generic Content Types

Posted by Community Admin on 03-Aug-2018 20:15

Querying Generic Content Types

All Replies

Posted by Community Admin on 09-Feb-2011 00:00
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()))
      ;


The problem I'm running into is that allItems is empty.  I'm 100% certain that searchTerm is set to a string that does exist in several generic content pieces.  How can I rewrite this to find content based on this search string?
Posted by Community Admin on 09-Feb-2011 00:00
Hello Chris,

Could you try the following

1. Check what 
manager.GetContent()
     .Where(n => n.Status == ContentLifecycleStatus.Live)
returns

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"))


Regards,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Posted by Community Admin on 09-Feb-2011 00:00
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.
Posted by Community Admin on 10-Feb-2011 00:00
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?
Posted by Community Admin on 10-Feb-2011 00:00
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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Posted by Community Admin on 10-Feb-2011 00:00
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));

This code does NOT work, even though in debug I can confirm that searchTerm is a string containing a word that does exist in several of the generic content items.

The following code DOES work:

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"));

This is obviously problematic since I need to be able to dynamically search.  
Posted by Community Admin on 22-Feb-2011 00:00
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
This thread is closed