Filter by Like

Posted by Community Admin on 04-Aug-2018 13:38

Filter by Like

All Replies

Posted by Community Admin on 03-Jun-2014 00:00

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

Posted by Community Admin on 04-Jun-2014 00:00

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

 

Posted by Community Admin on 04-Jun-2014 00:00

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

Posted by Community Admin on 04-Jun-2014 00:00

Thanks for the reply Stephen but SF doesn't like that: "Delegate does not take 1 argument"

Posted by Community Admin on 04-Jun-2014 00:00

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

Posted by Community Admin on 04-Jun-2014 00:00

Perfect!  Thanks

Posted by Community Admin on 05-Jun-2014 00:00

Stephen,

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

Posted by Community Admin on 05-Jun-2014 00:00

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...

Posted by Community Admin on 05-Jun-2014 00:00

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.

Posted by Community Admin on 06-Jun-2014 00:00

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
 

This thread is closed