Copied to clipboard

Flag this post as spam?

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


  • webmonger 130 posts 285 karma points
    Jun 02, 2011 @ 13:53
    webmonger
    0

    Add Time string to DateTime object in Where query

    Hi

    I'm writing a Query that needs to add to fields to one an other so a calculation can be made on 2 seperate fields held in a document.

    At the moment I have:

    var dateSpecificNodes = @Model.Children.Where("OpeningDate < DateTime.Now);

    What I need is something like

    var dateSpecificNodes = @Model.Children.Where("OpeningDate.Add(OpeningTime) < DateTime.Now");

    Where OpeningDate is formatted 02/06/2011 00:00:00 and OpeningTime is formatted 15:00

    Please can some one help me out.

    Cheers

    Jon

  • Richard Boelen 61 posts 153 karma points
    Jun 02, 2011 @ 17:25
    Richard Boelen
    0

    Can you try this?

    @{
        DynamicNodeList items = Model.Children;
        var dateSpecificNodes = items.OfType<dynamic>().Where(x => x.OpeningDate.Add(TimeSpan.Parse(x.OpeningTime)) < DateTime.Now);
    }

    Just make sure that OpeningTime is a string in the format: hh:mm:ss

    Cheers, Richard

     

  • webmonger 130 posts 285 karma points
    Jun 02, 2011 @ 18:10
    webmonger
    0

    Hi Richard

    It was a good call but I thought this might happen when you try that you recieve the "error 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" error.

    I also tried changing the code to but this did not work either

    allEventNodes.OfType<DynamicNode>().Where(x => x.OpeningDate.Add(TimeSpan.Parse(x.OpeningTime)) < DateTime.Now);

    I dont really fancy creating a Class every time I need to merge date and time fields.

    Any other ides?

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Jun 02, 2011 @ 18:30
    Jason Prothero
    0

    Is the OpeningDate field a string?  I think you may have to parse the date.

    Something like:

    var dateSpecificNodes = @Model.Children.Where("DateTime.Parse(OpeningDate) < DateTime.Now);
  • webmonger 130 posts 285 karma points
    Jun 02, 2011 @ 18:59
    webmonger
    0

    Hi Jason

    The example you provided returns this error: Expression of type 'System.Func`2[umbraco.MacroEngines.DynamicNode,System.Object]' cannot be used for parameter of type 'System.String' of method 'System.DateTime Parse(System.String)'

    So i figured ok if it thinks it trying to use and object as a string lets convert it to an string sor i tried:

    var dateSpecificNodes = @Model.Children.Where("DateTime.Parse(OpeningDate.ToString()) < DateTime.Now");

    This returned this error: Expression of type 'System.Func`2[umbraco.MacroEngines.DynamicNode,System.String]' cannot be used for parameter of type 'System.String' of method 'System.DateTime Parse(System.String)'

    Does anyone know if methods have access within the string style lambdas that are implemented here?

    If they are then I would be able to do

    var dateSpecificNodes = @Model.Children.Where("MyDateJoinFunction(OpeningDate, OpeningTime) < DateTime.Now");

    I'll have a look to see if this is possible.

    Cheers for the help

  • Richard Boelen 61 posts 153 karma points
    Jun 02, 2011 @ 19:30
    Richard Boelen
    0

    If the query gets more complex, you need to cast it to DynamicNodeList as the .Where string query is somewhat limited.

    With the DynamicNodeList you can do regular Linq queries. Not sure what datatype your OpeningDate and OpeningTime are, btw.

    Are they both DateTime?

    Richard

  • Richard Boelen 61 posts 153 karma points
    Jun 02, 2011 @ 19:44
    Richard Boelen
    0

    Checked again and this worked fine using updateDate as DateTime, the OfType(dynamic) drop out of my first reply:

    
    @using System.Linq;
    @{
        DynamicNodeList items = Model.Children;
        var dateSpecificNodes = items.OfType<dynamic>().Where(x => x.updateDate.Add(TimeSpan.Parse("00:15:00")) < DateTime.Now);
    }
    

     

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Jun 02, 2011 @ 20:05
    Jason Prothero
    0

    Bummer.  I'm still getting my head around this stuff as well.  Sounds like Richard's solution is likely the best way to go.

  • webmonger 130 posts 285 karma points
    Jun 03, 2011 @ 11:47
    webmonger
    0

    Thanks again Richard but I'm still having problems.

    Here is my current code

    DynamicNodeList allEventNodes = @Model.Children;

    var pagesToList = allEventNodes.OfType(dynamic).Where(x => x.updateDate.Add(TimeSpan.Parse(x.updateDate)) < DateTime.Now);

    When ever i try to run this i get the error "error CS0103: The name 'dynamic' does not exist in the current context"

    I've added the following name spaces to the code but still no joy

    @using umbraco.MacroEngines
    @using System.Linq
    @using System.Dynamic

    I'm lost. I've tried referencing dynamic else where in the file and it works without the error it just seems to be in this context it errors. Do you have any idea why this would happen?

    Cheers

  • Richard Boelen 61 posts 153 karma points
    Jun 03, 2011 @ 11:56
    Richard Boelen
    0

    Hi,

    I used OfType<dynamic>() instead of OfType(dynamic)

    notice the angled brackets, hope this helps...

  • webmonger 130 posts 285 karma points
    Jun 03, 2011 @ 14:51
    webmonger
    0

    Richard you're a genius that has sorted it.

    Now i need to work out how to get .Skip() and .Take() working with that object.

    Thanks so much for the help.

  • siva kumar 120 posts 209 karma points
    Oct 16, 2012 @ 11:51
    siva kumar
    0
Please Sign in or register to post replies

Write your reply to:

Draft