Copied to clipboard

Flag this post as spam?

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


  • jacob phillips 130 posts 372 karma points
    Jan 17, 2014 @ 00:04
    jacob phillips
    0

    simple question : compare a number

    I'm rewriting an XSLT macro to razor in Umbraco 6. I want to store the number of items from a multi-picker in a variable (recordCount), then compare it to zero. I've tried so many things and I can't figure it out. If  I take away this:

     

      @* this won't work *@
      if(recordCount > 0) {}

     

    and this:

    @* neither will this *@
    if(@typedMNTPCollection.Count() > 0) {}

    Everything is fine.

     

    What am I missing?

     

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core.Dynamics;
    @{
        if (Model.Content.HasValue("cprHighlights"))
        {
            var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue("cprHighlights"));
      
            var typedPublishedMNTPNodeList = new List();
            foreach (dynamic id in typedMultiNodeTreePicker)               
            {
                typedPublishedMNTPNodeList.Add(id.InnerText);
            }
            var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
            foreach (var item in typedMNTPCollection)
            {    
               

    @item.Name

            
       

    @item.Id

            
            }  
      var recordCount = @typedMNTPCollection.Count();
      
      @* this won't work *@
      if(recordCount > 0) { }
    @* neither will this *@
    if(@typedMNTPCollection.Count() > 0) {}

      
        }
    }
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 00:23
    Jeavon Leopold
    1

    Hi Jacob,

    I think the only issue is some additional @s, try this:

            foreach (var item in typedMNTPCollection)
            {     
    <p>              
        @item.Name       
        @item.Id
      </p>           
                }   
      var recordCount = typedMNTPCollection.Count();
    
      @* this won't work *@
      if(recordCount > 0) { }
    @* neither will this *@
    if(typedMNTPCollection.Count() > 0) {}
    

    Jeavon

  • jacob phillips 130 posts 372 karma points
    Jan 17, 2014 @ 00:32
    jacob phillips
    0

    Yes, that was it. Thank you for looking at this. I guess I need to take a break.

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 17, 2014 @ 01:02
    Jeavon Leopold
    0

    Awesome, actually the Any method is slightly more efficient, e.g.

    if(typedMNTPCollection.Any()) {}

    Jeavon

  • jacob phillips 130 posts 372 karma points
    Jan 17, 2014 @ 01:44
    jacob phillips
    0

    Why thank you! I will use that instead.

Please Sign in or register to post replies

Write your reply to:

Draft