Copied to clipboard

Flag this post as spam?

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


  • Simon Deighton 23 posts 53 karma points
    Mar 30, 2013 @ 15:58
    Simon Deighton
    0

    Using .Where to list only a certain child doctype

    So I have a page which has a few child pages of varying doctypes.

    Im wanting to display some data from just the pages that have Room as the doctype.

    After a little googling I found the below but im not getting anything displayed, no errors. Am I missing something?

    Thanks

    @foreach (var room in Model.Content.Children.Where("NodeTypeAlias ==  \"Room\""))
    {
      Stuff
    }
  • MK 429 posts 906 karma points
    Mar 30, 2013 @ 22:48
    MK
    0

    Hi Simon,

    Try: 

    @Model.AncestorOrSelf(1).Children.Where("NodeTypeAlias==\"Room\"").FirstOrDefault()

    I would prefer to use http://ucomponents.org/umapper/

    Also, you can always debug it.

    Regards,

    mkariti

  • Simon Deighton 23 posts 53 karma points
    Mar 31, 2013 @ 11:45
    Simon Deighton
    0

    Hi mkariti

    Im not sure on the correct usage of your suggested line

    When Ive copied that into studio Im getting an error -

    Umbraco.Web.Models.RenderModel' does not contain a definition for 'AncestorOrSelf'

     

    Does anyone know why my original line isnt correct? Im a bit new to Razor and I really thought that would work!

  • Simon Deighton 23 posts 53 karma points
    Mar 31, 2013 @ 12:19
    Simon Deighton
    0

    Also just to add if I do the snippet without the .where it does return all the pagesm including the ones I dont want but I guess im half way there?

  • Charles Afford 1163 posts 1709 karma points
    Mar 31, 2013 @ 12:29
    Charles Afford
    0

    Are you using razor mvc?  if so you ahould be able to do something like 

    @foreach(var room inModel.Content.Children.Where(x=>x.NodeTypeAlias ==  "Room")
    {
     
    Stuff
    }

    Charlie. :)

     

  • Simon Deighton 23 posts 53 karma points
    Mar 31, 2013 @ 13:36
    Simon Deighton
    0

    Yes its Razor MVC :) And you know what, that looks familiar hmmm

    Its moaning about <Umbraco.Core.Models.IPublishedContent,int,bool>' does not take 1 arguments however, any ideas?

  • Simon Deighton 23 posts 53 karma points
    Mar 31, 2013 @ 13:45
    Simon Deighton
    0

    FIXED IT WOOO my working line is now:

    @foreach (var room in Model.Content.Children.Where(x => x.DocumentTypeAlias == "Room"))
    {
      Stuff
    }

    Changed the NodTypeAlias to DocumentTypeAlias and its happy now :)

    Thanks both

  • Charles Afford 1163 posts 1709 karma points
    Mar 31, 2013 @ 13:51
    Charles Afford
    0

    yea sorry would be doc type alias :D.  Glad you solved it.  Charlie :)

  • Proxicode 128 posts 324 karma points
    Aug 07, 2013 @ 09:12
    Proxicode
    0

    Though I am using NodeTypeAlias still, I tried a slight tweak on Charlie's solution but it breaks.

    This line works

    var theKids = @Model.NodeById(@pageId).Children();
    

    but then I have to loop through with something like this, which works, but it's ugly

    foreach(var child in @theKids){    
        var docType = DocumentType.GetByAlias(@child.NodeTypeAlias).Text;    
        if(docType == "Event")    
        {    
            // do stuff    
        }    
    }
    

    If I could use some variation of Charlie's solution above...something like this

    var theKids = @Model.NodeById(@pageId).Children.Where(x => x.NodeTypeAlias.Text == "Event");
    

    Umbraco says

    "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

    Thanks for any pointers you all may have.

    -Roger

  • Leigh 4 posts 24 karma points
    Jul 31, 2014 @ 15:36
    Leigh
    0

    Hi Roger, Did you ever find a solution to your error?

  • Proxicode 128 posts 324 karma points
    Jul 31, 2014 @ 18:13
    Proxicode
    0

    Hi Leigh,

    It's to do with data type casting, I was using var and not casting the results was the problem if I recall. Today I would write it something like this.

    using umbraco.NodeFactory;
    
    List<Node> events = 
         @Model.NodeById(@pageId).Children.Where(x => x.NodeTypeAlias.Text == "Event").ToList();
    

    There is a good post here about using generics to achieve this if that's an option for you. http://stackoverflow.com/questions/21388447/lambda-expression-as-an-argument-to-a-dynamically-dispatched-operation

    -Roger

Please Sign in or register to post replies

Write your reply to:

Draft