Copied to clipboard

Flag this post as spam?

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


  • Alwyn Rhodes-Mays 5 posts 35 karma points
    Jan 15, 2015 @ 00:30
    Alwyn Rhodes-Mays
    0

    Lost in Lambda expressions...

    Hello all,

    As a relative newbie to Umbraco and especially Lambda expressions, I've been struggling with something that should be so simple (which I understand to be the point of Lambda expressions!), but it's taken me on a road to nowhere, and after googling and trying many things, I've more or less given in.

    All I want to do is return a list of nodes that match a date criteria.  Basically I have a document type with a date picker control on it, and I want to return all nodes that have a date > today.

    I therefore expected the Lambda expression to be something like;

    var specificnodes = Model.Content.Where(x => x.eventDate.Value < DateTime.Now);

    but I get an exception error of "Cannot convert lambda expression to type 'string' because it is not a delegate type".  I assume this is due to me needing to convert something somewhere, but I've tried millions of combinations of .value and so on, but dont really understand what the issue is.   

    Can anyone make sense of this?   I'm using Umbraco 7.2.

    Thanks in advance!!

    Alwyn

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 15, 2015 @ 08:04
    Dennis Aaen
    1

    Hi Alwyn,

    I think that you can do something like this

    DateTime today = DateTime.Now;

    @foreach(var page in Model.Content.Children.Where(x => x.IsVisible()).Where(
    x => x.GetPropertyValue<DateTime>("PropertyAlias") < @0", today)){
    ....
    }

    In my example you need to change the PropertyAlias in the where so it match your property alias for the date picker on the document type I think it´s the eventDate from your post. In this example above is take the children of the page that you are adding the marco to.

    If you have an overview page, and the underneath you have the items that you want in the list then you should place the macro on the template for overview page. I have not tested the code myself, but hope this can help you or at least a step forward.

    Hope this helps,

    /Dennis

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 09:43
    Jeavon Leopold
    103

    Hi Alwyn,

    You are very close, it should be like this:

    var specificnodes = Model.Content.AncestorOrSelf(1).Children.Where(x => x.GetPropertyValue<DateTime>("eventDate") < DateTime.Now);
    

    The part AncestorOrSelf(1).Children might be wrong, it depends on where the nodes you want are located in your tree.

    Jeavon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 09:50
    Jeavon Leopold
    0

    By the way, did you try the query builder (new button in the template editor) to create this for you?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 09:57
    Jeavon Leopold
    0

    @Dennis FYI, you can't use the @0 token syntax in a lambda, that's for magic Where strings that are generally only used with the dynamic model (CurrentPage)

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 15, 2015 @ 10:01
    Dennis Aaen
    1

    @Jeavon  Arh okay, then I got a little bit wiser today, thanks for your good explanation. Most of the time I write the dynamic model (CurrentPage) :-)

  • Alwyn Rhodes-Mays 5 posts 35 karma points
    Jan 15, 2015 @ 12:53
    Alwyn Rhodes-Mays
    0

    Thanks for your help Jeavon  and Dennis - very much appreciated!!!

    I can't believe that I didnt realise there was a query builder for this, although it still doesnt seem to work that well with dates in Umbraco, in terms of identifying the type differently, and also, it seemed to traverse the tree, rather than using a Lambda expression, which I could have done, but I wanted to understand how to do it more efficiently using a Lambda, as that is what it seemed to be for.

    Alwyn

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 13:31
    Jeavon Leopold
    0

    Great!

    Yes the query builder, but as you say it needs a date picker (there is a issue for that here). Anyway it will generate dynamics (CurrentPage) so not lambda expressions which is what you're after!

Please Sign in or register to post replies

Write your reply to:

Draft