Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi there,
Within a razor template, I have 3 query parameters that can have a value or be empty strings:
string type = Request["type"] != null ? Request["type"] : ""; string year = Request["year"] != null ? Request["year"] : ""; string industry = Request["industry"] != null ? Request["industry"] : "";
And I want to query all the posts that matches these parameters if they are not empty strings.
So I need to build some dynamic where clauses.
I have tried a few things but without success. This is how I query my posts now. That would need to be completed with my dynamic where clauses.
var latestNews = Umbraco.Content(Guid.Parse("24c08a24-217f-4b25-a3e8-df322c4b0d16")) .ChildrenOfType("insightsOrNewsPost") .Where(x => x.IsVisible())
Anyone has an idea how to do this?
Thanks
Please can you try below variation:-
var latestNews = Umbraco.Content(Guid.Parse("24c08a24-217f-4b25-a3e8-df322c4b0d16")) .ChildrenOfType("insightsOrNewsPost") .Where(x => x.IsVisible() && (!string.IsNullOrEmpty(type) || x.type == type) && (!string.IsNullOrEmpty(year) || x.year == year ) && (!string.IsNullOrEmpty(industry) || x.industry == industry ) )
Cheers,
Shaishav
I've tried with only one parameter like this:
var latestNews = Umbraco.Content(Guid.Parse("24c08a24-217f-4b25-a3e8-df322c4b0d16")) .ChildrenOfType("insightsOrNewsPost") .Where(x => x.IsVisible() && x.Id != featuredPost.Id && (!string.IsNullOrEmpty(type) || x.Value<string>("type") == type) )
But it returns no results when type is an empty string. And returns all the visible posts without applying the filter when it's not empty.
Hi Arno,
Please can you try below.
var latestNews = Umbraco.Content(Guid.Parse("24c08a24-217f-4b25-a3e8-df322c4b0d16")) .ChildrenOfType("insightsOrNewsPost") .Where(x => x.IsVisible() && x.Id != featuredPost.Id && (string.IsNullOrEmpty(type) || x.Value<string>("type") == type) )
Legend that's the one.
Thanks a lot for your help !
Best, Arno
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
How to build dynamic where clauses?
Hi there,
Within a razor template, I have 3 query parameters that can have a value or be empty strings:
And I want to query all the posts that matches these parameters if they are not empty strings.
So I need to build some dynamic where clauses.
I have tried a few things but without success. This is how I query my posts now. That would need to be completed with my dynamic where clauses.
Anyone has an idea how to do this?
Thanks
Please can you try below variation:-
var latestNews = Umbraco.Content(Guid.Parse("24c08a24-217f-4b25-a3e8-df322c4b0d16")) .ChildrenOfType("insightsOrNewsPost") .Where(x => x.IsVisible() && (!string.IsNullOrEmpty(type) || x.type == type) && (!string.IsNullOrEmpty(year) || x.year == year ) && (!string.IsNullOrEmpty(industry) || x.industry == industry ) )
Cheers,
Shaishav
I've tried with only one parameter like this:
But it returns no results when type is an empty string. And returns all the visible posts without applying the filter when it's not empty.
Hi Arno,
Please can you try below.
Legend that's the one.
Thanks a lot for your help !
Best, Arno
is working on a reply...