Copied to clipboard

Flag this post as spam?

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


  • Tom 161 posts 322 karma points
    Mar 19, 2015 @ 13:16
    Tom
    0

    Access Custom template properties in a Macro

    I have the following code that works but I would like to combine the if statement in the foreach where clause
    Note:  
    newsCategory  is a custom property on my newsitem template

      @foreach (var page in CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc"))
        { 
    if (page.newsCategory == "Articles")
    {
    ....
    }

     

    How do I combine the if condition in my where clause?  

    Thanks
    Tom
  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Mar 19, 2015 @ 15:30
    Tim
    1

    If you wanted to do it with strongly typed models rather than the dynamic ones, the code would be:

    @foreach (var page in Model.Content.Children.Where(a => a.IsVisible() && a.GetPropertyValue("newsCategory","") == "Articles").OrderBy(b => b.CreateDate))
    {
    .......

    I'm not 100% on the dynamic syntax though. I generally prefer to use the strongly typed stuff, as you get intellisense.

    Hope that helps!

  • Tom 161 posts 322 karma points
    Mar 19, 2015 @ 19:26
    Tom
    0

    I like that.  Thanks

     

Please Sign in or register to post replies

Write your reply to:

Draft