Copied to clipboard

Flag this post as spam?

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


  • Tatiana Vassilieva 18 posts 98 karma points
    Jul 16, 2013 @ 08:22
    Tatiana Vassilieva
    0

    Date ranges in .Where() queries? (v4.11)

    Can someone tell me why this does not work??:
    var date = DateTime.Now.AddDays(7).Date;
    var collection = @stuff.Where("startDate< "+@date);

    While this does:
    var collection = @stuff.Where("startDate< DateTime.Now.AddDays(7).Date");


    I mean, it's a pain to write the whole thing when have multiple dates... if I add .Day or .Month to both dates it will work, but of course .Date totally fails so how do I get @date to the right format the get this query to recognise it?

  • Tatiana Vassilieva 18 posts 98 karma points
    Jul 16, 2013 @ 08:36
    Tatiana Vassilieva
    0

    This also works: @stuff.Where("startDate < DateTime("[email protected]+","[email protected]+","[email protected]+").Date");

    Which I guess is applicable for what I want to do, but is there no better way?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 16, 2013 @ 13:10
    Jeavon Leopold
    0

    Hi Tatiana,

    Looks like you've worked it out yourself, it doesn't work when you are adding it to the string because it becomes a string rather than a DateTime object.

    You could do something along these lines to keep it fully typed and not involve any strings:

         var collection = new DynamicNode(-1).Descendants().Where(x => x.GetPropertyValue("startDate").AsDateTime() < DateTime.Now.AddDays(7) && x.GetPropertyValue("startDate") != null);
        foreach (dynamic page in collection)
        {
            <p>@page.Name</p>
        }

     Jeavon

  • Tatiana Vassilieva 18 posts 98 karma points
    Jul 29, 2013 @ 03:53
    Tatiana Vassilieva
    0

    Thanks :)
    So it's not able to convert the string to a date in the query then? Like DateTime.Parse()?

    I was looking for ways to pass the string because I was retrieving collections for the "current week" and "current month", with the possibility that I would need to pass custom dates through web requests later.


    e.g.
    var diff = 0 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"));

    var events_this_week = @events.Where("Visible && startDate>=DateTime.Now.AddDays("+@diff+").Date && startDate<=DateTime.Now.AddDays("+@diff+").AddDays(7).Date").OrderBy("startDate");

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 29, 2013 @ 12:05
    Jeavon Leopold
    0

    Yes you can, it would be something like this (1011, is the node id of the parent of your events):

        var events = new DynamicNode(1011).Children;    
        var diff = 0 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"));
    
        var events_this_week = events.Where(x => x.Visible && x.GetPropertyValue("startDate") != null && x.GetPropertyValue("startDate").AsDateTime() >= DateTime.Now.AddDays(diff) && x.GetPropertyValue("startDate").AsDateTime() <= DateTime.Now.AddDays(diff).AddDays(7)).OrderBy(x => x.GetPropertyValue("startDate").AsDateTime());
    
        foreach (dynamic page in events_this_week)
        {
            <p>@page.Name</p>
        } 

     

  • Tatiana Vassilieva 18 posts 98 karma points
    Aug 26, 2013 @ 08:31
    Tatiana Vassilieva
    0

    Finally got around to trying doing a node collection this way while doing a search filter... took me a while to realise needed to cast that my parent node as a "new DynamicNode(@parentNode.Id)" for anything to even start working!


    Which is to say, it gave me an error: "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type". A good thing to note for future reference :)

     

    What is the datatype of a node collection anyway? Because I would like to create an empty collection that would return 0 on Count() without cheating with Take(0)...

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 26, 2013 @ 10:02
    Jeavon Leopold
    0

    Yes you can't use a lambda expression with dynamics which is why you need to use new DynamcNode

    I think what you are looking for is DynamicNodeList

Please Sign in or register to post replies

Write your reply to:

Draft