+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Filter by Like

Filter by Like

10 posts, 1 answered
  1. John Rosen Jr
    John Rosen Jr avatar
    6 posts
    Registered:
    18 Dec 2008
    03 Jun 2014
    Link to this post

    I have a custom content type and have the code snippet for "Company ="

    var myFilteredCollection = dynamicModuleManager.GetDataItems(p2020Type).Where("Company = \"Some Company\"");

    How do I filter the collection by the first letter of each company passed in via querystring -
    Company LIKE 'A%'

    Thanks

  2. Stephen
    Stephen avatar
    4 posts
    Registered:
    27 Apr 2012
    04 Jun 2014 in reply to John Rosen Jr
    Link to this post

    Hey, use Lambda expressions to filter, unless you really need Dynamic LINQ as a string, e.g.,:

    var myFilteredCollection = dynamicModuleManager.GetDataItems(p2020Type).Where(o => o.Company.StartsWith("A"));

     

  3. Stephen
    Stephen avatar
    4 posts
    Registered:
    27 Apr 2012
    04 Jun 2014 in reply to Stephen
    Link to this post

    Don't forget you almost certainly want LIVE records only, e.g.,:

    var myFilteredCollection = dynamicModuleManager.GetDataItems(p2020Type).Where(o => o.Visible && o.Status == ContentLifecycleStats.Live && o.Company.StartsWith("A"));

  4. John Rosen Jr
    John Rosen Jr avatar
    6 posts
    Registered:
    18 Dec 2008
    04 Jun 2014 in reply to Stephen
    Link to this post
    Thanks for the reply Stephen but SF doesn't like that: "Delegate does not take 1 argument"
  5. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    04 Jun 2014
    Link to this post

    Heh, sorry, you're 100% correct, I apologize.  DynamicTypes aren't strongly typed. Here's the code you need:

    public static IQueryable<DynamicContent> GetDynamicContentItemsAsQueryable(string type)
            {
                var itemType = TypeResolutionService.ResolveType(string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", type));
                return DynamicModuleManager.GetManager().GetDataItems(itemType).Where(o => o.Visible && o.Status == ContentLifecycleStatus.Live);
            }

    Call it like:

    var myFilteredCollection =GetDynamicContentItemsAsQueryable("YourType.P2020TypeWhatever").Where(o => o.GetValue<string>("Company").ToLower().StartsWith("a"));

     

    Hope it works this time!! Sorry for the false start

    Answered
  6. John Rosen Jr
    John Rosen Jr avatar
    6 posts
    Registered:
    18 Dec 2008
    04 Jun 2014 in reply to Stephen2
    Link to this post
    Perfect!  Thanks
  7. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    05 Jun 2014 in reply to John Rosen Jr
    Link to this post

    Stephen,

    What is the benefit of using lambda expression over Dynamic LINQ?  Are we talking about a significant performance difference or something else entirely?

  8. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    05 Jun 2014
    Link to this post

    Sorry Stacey, I don't have an informed opinion on performance.  I was just suggesting that the "normal" lambda method should be generally easier to write, read and you get strongly typed - so overall better.

     Although having done Zero research, I'd think that parsing a string to turn into an actual Linq query has some performance hit involved too...

  9. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    05 Jun 2014 in reply to Stephen2
    Link to this post

    That's ok.  I just thought there might be something I was missing.  I typically have been using lambda expressions, but I had noticed that the generated docs do not and I have experimented on a few cases with using them.

  10. Kaloyan
    Kaloyan avatar
    11 posts
    Registered:
    19 Sep 2016
    06 Jun 2014
    Link to this post
    Hi,

    Let us know if you are facing any further issues.

    Regards,
    Kaloyan
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
10 posts, 1 answered