Filter by Like
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
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"
));
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"
));
Thanks for the reply Stephen but SF doesn't like that: "Delegate does not take 1 argument"
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
Perfect! Thanks
Stephen,
What is the benefit of using lambda expression over Dynamic LINQ? Are we talking about a significant performance difference or something else entirely?
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...
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.
Hi,
Let us know if you are facing any further issues.
Regards,
Kaloyan
Telerik