I tried exactly your code andgot an error, I also tried this as I only need the direct children and I'd found an example similar to yours using .Children
ompiler Error Message: CS1928: 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'Where' and the best extension method overload 'Umbraco.Web.PublishedContentExtensions.Where(Umbraco.Core.Models.IPublishedContent, string, string)' has some invalid arguments
CS0119: 'Umbraco.Web.PublishedContentExtensions.Children(Umbraco.Core.Models.IPublishedContent)' is a 'method', which is not valid in the given context
'Umbraco.Web.Models.RenderModel' does not contain a definition for 'Children' and the best extension method overload 'Umbraco.Web.PublishedContentExtensions.Children(Umbraco.Core.Models.IPublishedContent)' has some invalid arguments
Since the forum editor has botched much of the content here, I'll go out on a limb with a suggestion that might have been made already (albeit with the formatting and visibility retained, if possible). You could try passing the values instead of using literals in the string, I know a dictionary approach was suggested but this more primitive method might still be supported in your version:
However, I'm slightly confused at your use of `articleDate` since, unless this is a badly-cased property, isn't this something you could check a priori to this query?
Okay, much of my suggestion was rubbish because only queryMonth is actually a 'variable' here. In terms of badly-cased, I wasn't sure of the sope of articleDate, whether it was a local variable in scope or a property of the document type - to my knowledge Umbraco doesn't care re casing of property name / alias references but humans do and conventionally camel-casing is indicative of a variable, not a property, hence potential confusion.
Anyway, if everything works bar when you swap out the twelve for a variable (queryMonth), try what I suggested but only for that value, and furthermore, you could try string comparisons instead:
not sure how secure that would be when queryMonth is taken from a user parameter, I presume the Children.Where method would handle that side of things?
Thanks for your help, I'm struggling to find much documentation on the MVC rendering for Umbraco other than the Razor snippets and Walkthrough.
Okay, great, a variation that works, well done! As for user input given via query string values, check it's a valid type and within range. For example:
int month = 0; var queryMonth = Request.QueryString["m"]; if (!string.IsNullOrEmpty(queryMonth) && int.TryParse(queryMonth, out month) && month >= 1 && month <= 12) { // given value is a valid integer in supported range of 1 and 12 so proceed using 'month' }
Where clause works with hard coded values but not values from variables
Hi,
got a strange problem I hope someone can help with, I'm filtering some pages like this:
This work fine but if i put a variable in place of 12 for the month it never returns any results eg:
I've checked that queryMonth does contain 12, tried converting the type. whatever I've tried only works with 12 hard coded, any help much appreciated.
Regards
You have to do this:
Dictionary<string,object> values = new Dictionary<string,object>();
values.Add("queryMonth", DateTime.Now.Month);
var child = parent.DescendantsOrSelf().Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month == queryMonth", values);
Oh you can yse a LINQ Where calause like so:
parent.DescendantsOrSelf().Items.Where(node => node.articleDate.Year == DateTime.Now.Year && node.articleDate.Month == DateTime.Now.Month);
Thanks Jeremy
I tried exactly your code andgot an error, I also tried this as I only need the direct children and I'd found an example similar to yours using .Children
foreach(var news in Model.Content.Children.Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month == queryMonth", values) ){
ompiler Error Message: CS1928: 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'Where' and the best extension method overload 'Umbraco.Web.PublishedContentExtensions.Where(Umbraco.Core.Models.IPublishedContent, string, string)' has some invalid arguments
using .Content is getting you a different result. jsut use .Children
IE:
Model.Children.Items.Where()
I did try that, it gives this error:
CS0119: 'Umbraco.Web.PublishedContentExtensions.Children(Umbraco.Core.Models.IPublishedContent)' is a 'method', which is not valid in the given context
Add ()'s , Children()
I'm not having much luck with this:
var values = new Dictionary
<string,object>
();
values.Add("queryMonth", DateTime.Now.Month);
foreach(var news in Model.Children().Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month == queryMonth", values ) ){
Produces:
'Umbraco.Web.Models.RenderModel' does not contain a definition for 'Children' and the best extension method overload 'Umbraco.Web.PublishedContentExtensions.Children(Umbraco.Core.Models.IPublishedContent)' has some invalid arguments
Hi,
Anybody have any other ideas, I didn't get any further unfortunately...
Regards
Since the forum editor has botched much of the content here, I'll go out on a limb with a suggestion that might have been made already (albeit with the formatting and visibility retained, if possible). You could try passing the values instead of using literals in the string, I know a dictionary approach was suggested but this more primitive method might still be supported in your version:
However, I'm slightly confused at your use of `articleDate` since, unless this is a badly-cased property, isn't this something you could check a priori to this query?
Hi Grant,
That formatting problem happens when using this forum with IE8....
why is articleDate badly cased? that's how Umbraco store the alias? What are you suggesting should be checked prior to the query?
regards
Hi Grant
didn't work unfortunately, CS0103: The name 'articleDate' does not exist in the current context
I'm using:
umbraco v 4.11.1 (Assembly version: 1.0.4715.27659)
Okay, much of my suggestion was rubbish because only queryMonth is actually a 'variable' here. In terms of badly-cased, I wasn't sure of the sope of articleDate, whether it was a local variable in scope or a property of the document type - to my knowledge Umbraco doesn't care re casing of property name / alias references but humans do and conventionally camel-casing is indicative of a variable, not a property, hence potential confusion.
Anyway, if everything works bar when you swap out the twelve for a variable (queryMonth), try what I suggested but only for that value, and furthermore, you could try string comparisons instead:
Or
Model.Content.Children.Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month.ToString() == @1", queryMonth.ToString())
Not entirely sure Umbraco supported doing ToString within the query literal, but try and see if the ToString of queryMonth matches either way.
Ok I see, Umbraco just puts the property aliases into camel case so I just let it...
Those didn't work but this did:
Model.Content.Children().Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month == " + queryMonth )
not sure how secure that would be when queryMonth is taken from a user parameter, I presume the Children.Where method would handle that side of things?
Thanks for your help, I'm struggling to find much documentation on the MVC rendering for Umbraco other than the Razor snippets and Walkthrough.
Regards
Okay, great, a variation that works, well done! As for user input given via query string values, check it's a valid type and within range. For example:
great thanks, but you'd expect that sql injection isn't a problem. It's querying the xml right?
Regards
is working on a reply...