Copied to clipboard

Flag this post as spam?

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


  • Osman Coskun 164 posts 398 karma points
    May 03, 2016 @ 21:42
    Osman Coskun
    0

    How to render n-th element or first element in nested content?

    I want to just the render first element of nested content.

    I managed to get the values with the code below, is there any other practical way?

    var infos = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("contactInfos");
    var i=0;
    foreach(var info in infos){
        if(i==0){
            @Umbraco.Field(info, "email")
        }
        i++;
    }
    

    Thanks!

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    May 03, 2016 @ 22:08
    Lee Kelleher
    101

    Hi Osman,

    You could try this...

    var info = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("contactInfos").FirstOrDefault();
    
    @info.GetPropertyValue("email")
    

    I hope this helps?

    Cheers,
    - Lee

  • Osman Coskun 164 posts 398 karma points
    Nov 28, 2016 @ 22:16
    Osman Coskun
    0

    Hi again Lee,

    I used your suggested solution on previous projects. However it doesn't work on Umbraco 7.5.4. and 7.5.3

    I get the following error.

    'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'FirstOrDefault'
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'FirstOrDefault'
    

    Any suggestions? Thanks in advance...

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 29, 2016 @ 09:31
    Lee Kelleher
    0

    Hi Osman,

    That's pretty strange.

    I'm not sure where the System.Collections.Generic.List part is coming from... is that in your code? The example above uses IEnumerable<IPublishedContent>.

    Cheers,
    - Lee

  • Osman Coskun 164 posts 398 karma points
    Dec 02, 2016 @ 21:21
    Osman Coskun
    0

    Hi Lee,

    What I'm trying to do is get the contact information of the first office (nested content data type) to display on footer area.

    I use the code:

    CurrentPage.AncestorOrSelf(1).Descendants("contactPage").First().GetPropertyValue<IEnumerable<IPublishedContent>>("offices").FirstOrDefault();
    

    What am i doing wrong?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Dec 05, 2016 @ 09:52
    Lee Kelleher
    0

    Hi Osman,

    I'm not totally sure what is going on... you say it did previously work, then you upgraded and it no longer worked?

    The actual error "does not contain a definition for 'FirstOrDefault'" usually means that the Razor view can't find the namespace for an extension method... so you could try adding this to the very top of your Razor script...

    @using System.Linq
    

    Hopefully that may work?

    Otherwise, from looking at your last reply, my only thought would be that the chaining of the extension methods may have a null reference at some point - it could be the .First() straight after the Descendants?

    You may need to break down the single line statement, so it can be debugged better?

    e.g.

    var root = CurrentPage.AncestorOrSelf(1);
    var contactPages = root.Descendants("contactPage");
    var contactPage = contactPages .First();
    var offices = contactPage.GetPropertyValue<IEnumerable<IPublishedContent>>("offices");
    var office = offices.FirstOrDefault();
    

    I know it's more lines of code... but it will help you identify where the exact problem is.

    Cheers,
    - Lee

  • Osman Coskun 164 posts 398 karma points
    Dec 15, 2016 @ 09:06
    Osman Coskun
    0

    Hi Lee,

    The client don't want head office contact info in the footer any more :)

    But i couldn't find where the error comes from.

    If i loop through offices, i get the values without any problems.

    var root = CurrentPage.AncestorOrSelf(1);
    var contactPages = root.Descendants("contactPage");
    var contactPage = contactPages .First();
    var offices = contactPage.GetPropertyValue<IEnumerable<IPublishedContent>>("offices");
    //var office = offices.FirstOrDefault();
    
    foreach(var office in offices){
        //do something
    }
    

    @using System.Linq didn't help either.

    Any ways thanks for helping. Best regards...

  • Osman Coskun 164 posts 398 karma points
    May 03, 2016 @ 22:20
    Osman Coskun
    0

    Great!

    Thanks for the tip. Is there a short method to get n-th element?

    Cheers :)

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    May 03, 2016 @ 22:27
    Lee Kelleher
    3

    Hi Osman,

    The easiest way would be to use the .Skip LINQ method.

    e.g. set the idx variable as the number before the one you want, so if it's the 4th, then set to 3.

    var idx = 3;
    var info = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("contactInfos").Skip(idx).FirstOrDefault();
    

    Hope this helps?

    Cheers,
    - Lee

  • Osman Coskun 164 posts 398 karma points
    May 03, 2016 @ 22:44
    Osman Coskun
    1

    Works :)

    Thank you very much Lee.

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Dec 05, 2016 @ 10:49
    Bjarne Fyrstenborg
    1

    Hi Osman

    As alternative you can also use ElementAtOrDefault to get a specific element in the collection:

    https://msdn.microsoft.com/library/bb494386.aspx

    https://www.dotnetperls.com/elementatordefault

    var idx = 3;
    var info = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("contactInfos").ElementAtOrDefault(idx);
    

    I am not sure it that is slower than the suggestion Lee mentioned.

    /Bjarne

  • Osman Coskun 164 posts 398 karma points
    Dec 15, 2016 @ 09:10
    Osman Coskun
    0

    Hi Bjarne,

    I get an error:

    'System.Collections.Generic.List

    That portion of code is not needed on the project. I'll try it on next one :)

    Thanks for helping...

Please Sign in or register to post replies

Write your reply to:

Draft