More in this section

Forums / General Discussions / Querying Generic Content Types

Querying Generic Content Types

7 posts, 0 answered
  1. Chris
    Chris avatar
    63 posts
    Registered:
    05 Jan 2011
    09 Feb 2011
    Link to this post
    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?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Feb 2011
    Link to this post
    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
  3. Chris
    Chris avatar
    63 posts
    Registered:
    05 Jan 2011
    09 Feb 2011
    Link to this post
    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.
  4. Chris
    Chris avatar
    63 posts
    Registered:
    05 Jan 2011
    10 Feb 2011
    Link to this post
    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?
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    10 Feb 2011
    Link to this post
    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
  6. Chris
    Chris avatar
    63 posts
    Registered:
    05 Jan 2011
    10 Feb 2011
    Link to this post
    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.  
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    22 Feb 2011
    Link to this post
    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
7 posts, 0 answered