Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt Jones 30 posts 141 karma points
    Dec 13, 2012 @ 14:38
    Matt Jones
    0

    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:

     

    Model.Content.Children.Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month == 12")

    This work fine but if i put a variable in place of 12 for the month it never returns any results eg:

    var queryMonth = DateTime.Now.Month;

    Model.Content.Children.Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month == queryMonth")

     

    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


     

  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Dec 13, 2012 @ 15:04
    Jeremy Pyne
    0

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

  • Matt Jones 30 posts 141 karma points
    Dec 13, 2012 @ 16:37
    Matt Jones
    0

    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

     

  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Dec 13, 2012 @ 16:57
    Jeremy Pyne
    0

    using .Content is getting you a different result.  jsut use .Children

    IE: 

     

    Model.Children.Items.Where()

  • Matt Jones 30 posts 141 karma points
    Dec 13, 2012 @ 18:09
    Matt Jones
    0

    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

  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Dec 13, 2012 @ 19:24
    Jeremy Pyne
    0

    Add ()'s , Children()

  • Matt Jones 30 posts 141 karma points
    Dec 13, 2012 @ 20:00
    Matt Jones
    0

    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

     

  • Matt Jones 30 posts 141 karma points
    Dec 30, 2012 @ 20:00
    Matt Jones
    0

    Hi,

     

    Anybody have any other ideas, I didn't get any further unfortunately...

     

    Regards

  • Grant Thomas 291 posts 324 karma points
    Dec 31, 2012 @ 12:16
    Grant Thomas
    0

    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:

     

    Model.Content.Children.Where("Visible && @1 == @2 && @3 == @4", articleDate.Year, DateTime.Now.Year, articleDate.Month, queryMonth)

    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?

  • Matt Jones 30 posts 141 karma points
    Dec 31, 2012 @ 12:31
    Matt Jones
    0

    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

  • Matt Jones 30 posts 141 karma points
    Dec 31, 2012 @ 12:51
    Matt Jones
    0

    Hi Grant

    didn't work unfortunately, CS0103: The name 'articleDate' does not exist in the current context

  • Matt Jones 30 posts 141 karma points
    Dec 31, 2012 @ 12:52
    Matt Jones
    0

    I'm using:

     

    umbraco v 4.11.1 (Assembly version: 1.0.4715.27659)

     

     

  • Grant Thomas 291 posts 324 karma points
    Dec 31, 2012 @ 12:59
    Grant Thomas
    0

    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:

     

    Model.Content.Children.Where("Visible && articleDate.Year == DateTime.Now.Year && articleDate.Month == @1", queryMonth)

    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.

     

  • Matt Jones 30 posts 141 karma points
    Dec 31, 2012 @ 13:28
    Matt Jones
    100

    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

     

     

     

  • Grant Thomas 291 posts 324 karma points
    Dec 31, 2012 @ 13:50
    Grant Thomas
    0

    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'
  • Matt Jones 30 posts 141 karma points
    Dec 31, 2012 @ 18:51
    Matt Jones
    0

    great thanks, but you'd expect that sql injection isn't a problem. It's querying the xml right?

     

    Regards

Please Sign in or register to post replies

Write your reply to:

Draft