Copied to clipboard

Flag this post as spam?

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


  • Giles Dermody 14 posts 35 karma points
    Aug 05, 2011 @ 11:52
    Giles Dermody
    0

    Using .Where() to find children with a certain datepicker value

    I'm currently rewriting an XSLT macro  to display child nodes of the current page, depending on what querystring variables are set for 'month' and 'year'. This is used for a news listings page which displays articles for a certain period.

    In the old macro, I am looping through and selecting nodes where the month part of the "newsDate" property (which is a datepicker field) and assigning them to the nodelist variable. $Displaymonth is gathered from  querystring.

    <xsl:for-each select="$currentPage/*[@isDoc]">
              <xsl:sort order="descending" select="newsDate" data-type="text"/>
              <xsl:if test="umbraco.library:FormatDateTime(newsDate, '%M') = $displayMonth">
                <xsl:copy-of select="." />
              </xsl:if>
    </xsl:for-each>

    I am having trouble creating a similar list of nodes using razor syntax. Assuming the querystring month is August, I have tried things like

    Model.Children.Where(umbraco.library.FormatDateTime(newsDate,'M') + " == 8");
    Model.Children.Where("Convert.ToDateTime(newsDate).Month == \"8\"");
    Model.Children.Where("newsDate.Month == \"8\"");
    Model.Children.Where("newsDate.Value.Month == \"8\"");

    The debug errors mostly complain that there is no property "month" inside my newsDate variable. Either that or "No property or field date exists in type 'Func`2'". It seems to be treating my Datepicker property as a string whatever I do, as described here but I am using the latest version of umbraco.

    How can I find children by converting the month/year of a datepicker property and comparing that to a variable? How can I even get this date property and extract the month/year, while inside a .Where statement?

  • Rajeev 126 posts 154 karma points
    Aug 05, 2011 @ 12:16
    Rajeev
    0

    Could you try this?

    Model.Children.Where(i=>Convert.ToDateTime(i.GetProperty("newsDate").Value).Month==8))

    -Rajeev

     

  • Giles Dermody 14 posts 35 karma points
    Aug 05, 2011 @ 12:20
    Giles Dermody
    0

    CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

  • Rajeev 126 posts 154 karma points
    Aug 05, 2011 @ 13:45
    Rajeev
    0

    Sorry, I just haven't tried that. That won't work. :(.

    You can try using if condition like you are doing in XSLT. There may be better ways  for doing this. I couldn't ge this work with Where. :(

       var [email protected];
        foreach(var item in list)
        {
             if(Convert.ToDateTime(item.GetProperty("newsDate").Value).Month.ToString()=="8")
             {
                 @item.Name

             }
       }

     you may get better answers from experts here.

    -Rajeev

Please Sign in or register to post replies

Write your reply to:

Draft