Copied to clipboard

Flag this post as spam?

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


  • Facius 12 posts 73 karma points
    Nov 17, 2011 @ 15:31
    Facius
    0

    Razor, .Where (DatetTime Greater Than Or Equal DateX )

    Hello Folks.

    I've been messing quite alot with this problem.

    I can get this to work...

    .Where("dateAndTime == DateTime.Now.Date")

    But what i really want is to find all  child nodes where my DateTime Property is Greater Or Equal (>=) to a specific date.

    Tried the following, and much more...

    DateTime futureDate = Convert.ToDateTime("2011-11-17");

    dynamic events = Model.Children.
    Where("dateAndTime >= futureDate.Date");

    Seems logic to me, but gets me this: The binary operator GreaterThanOrEqual is not defined for the types 'System.Object' and 'System.Object'.

    The Property is a "Date Picker with time", and of Database Type "Date", this can be changed if theres no solution with these.

    Hope you can give me a hint / solution :)

    Best Regards. Victor

     


  • Jon Hoye 19 posts 25 karma points
    Nov 17, 2011 @ 18:22
    Jon Hoye
    0

    Try:

     

    .Where("dateAndTime  >  DateTime.Parse(\"11/10/2011\")")

  • Facius 12 posts 73 karma points
    Nov 18, 2011 @ 13:10
    Facius
    0

    hmm.. didn't work. just returns zero results, but no errors..

  • Rodion Novoselov 694 posts 859 karma points
    Nov 18, 2011 @ 13:28
    Rodion Novoselov
    2

    What if to try:

    dynamic events =Model.Children.Where("dateAndTime >= @0", someDateTime.Date);
  • Facius 12 posts 73 karma points
    Nov 18, 2011 @ 13:44
    Facius
    0

    Nice! That worked.

    Actually also got Jon's way to work as well.

    Here's the to solutions i got running:

    DateTime futureDate Convert.ToDateTime("19-11-2011");
    dynamic Children @Model.Descendants("Event").Where("dateAndTime >= @0"futureDate);
    dynamic Children @Model.Descendants("Event").Where("dateAndTime > DateTime.Parse(\"19-11-2011 13:27:25\")");

    Thanks

  • Wayne 15 posts 72 karma points
    Dec 13, 2011 @ 01:03
    Wayne
    0

    Hi,

    Thanks for finding the solution to my issue.

    Although I use the following code, which is basically the same above, (dateTime being a date picker with time)

    DateTime date = DateTime.Now;

    dynamic Children = @item.Children.Where("active").Where("dateTime >= @0", date);
     

    I get this error

       Error loading Razor Script test.cshtml '.' or '(' expected

    Which certainly related to the date picker property evaluation, as if I remove it and put .Where("@0 >= @0", date) for exapmle no script error occurs.

    Any ideas, anyone ?

    This is on 4.7.0 by the way.

  • Rodion Novoselov 694 posts 859 karma points
    Dec 13, 2011 @ 18:24
    Rodion Novoselov
    0

    Hi, Wayne. Afaik, macro parameters don't support datetime directly, so your datetime picker is most probably converted to some other type. What data type do you use for your macro parameter?

  • Wayne 15 posts 72 karma points
    Dec 14, 2011 @ 01:18
    Wayne
    0

    Hi Rodion,

    its not a macro parameter, its part of the node property data.

    I've confirmed that the data type is a DateTime by adding a parameter call

    e.g Where("dateTime.value >= @0", date);

    and getting 'value not a property of DateTime datatype'

    also this working correctly

    var d = dateTime.Month

    <p>@d</p>

  • Rodion Novoselov 694 posts 859 karma points
    Dec 14, 2011 @ 01:37
    Rodion Novoselov
    0

    Hi. I think that simple replacing "date" with "Convert.ToDateTime(data)" should help.

  • Wayne 15 posts 72 karma points
    Dec 14, 2011 @ 05:00
    Wayne
    0

    same result.

    whenever I access the dateTime property (which is a date picker with time, I get

    Error loading Razor Script test.cshtml '.' or '(' expected

    if I try and convert it (which I know it already is a DateTime data type) or use it directly it fails with this error.  thats is only if I access it within the Where method

     

     

    W

  • Rodion Novoselov 694 posts 859 karma points
    Dec 14, 2011 @ 10:46
    Rodion Novoselov
    0

    Ah, I understand. Your "date" is of the DateTime type and your "dateTime" is a datetime picker, right? So, actually the "dateTime" is a string that's actually a datetime formatted in a sortable "s" format (yyyy-MM-ddThh:mm). Your just need vice versa convert the "date" to a string with the sortable format too:

    ...Where("dateTime >= @0", date.ToString("s"));

  • Eduardo Sobrinho 18 posts 38 karma points
    Jan 26, 2015 @ 19:57
    Eduardo Sobrinho
    0

    Hello. This post helped me a lot and it worked perfectly. Thank you very much!

    I created a docType called 'Evento' with a custom property called 'data' and it is formatted with date and time.
    I need to get "today's" events even if your time is in the past. Could someone help me change the code below to make it work?

    (forgive my bad English. I'm Brazilian)

    var startNodeID = Parameter.nodeId;
    var father = Library.NodeById(startNodeID);
    // Get all descendants of type EVENTO:
    DateTime dataLimit = DateTime.Now;
    var eventos = father.Descendants("Evento").OrderBy("data desc").Where("data >= @0", dataLimit).Take(4);
Please Sign in or register to post replies

Write your reply to:

Draft