Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1289 posts 2746 karma points
    Sep 07, 2021 @ 20:58
    Amir Khan
    0

    Generate list from ienumberable

    I feel like I'm missing something super simple but is there a way to generate a csv from an Ienmerable without doing a foreach loop? I need to store the values of the names of nodes within the ienumberable into one var somehow.

    So I have this which queries a MNTP

    var industries = item.GetPropertyValue<IEnumerable<IPublishedContent>>("industries");
    

    And the output I want is "industrynode1, industrynode2, etc"

    Thanks! Amir

  • Jamie Townsend 62 posts 287 karma points c-trib
    Sep 07, 2021 @ 22:25
    Jamie Townsend
    1

    Hi Amir,

    Not tested, but maybe something like this.

    var industries = item.GetPropertyValue<IEnumerable<IPublishedContent>>("industries");
    if (industries != null && industries.Any()){
        var commaList = string.Join(",", industries.Select(x => x.Name));
    }
    
  • Amir Khan 1289 posts 2746 karma points
    Sep 08, 2021 @ 14:09
    Amir Khan
    0

    I feel like that's close but its throwing this: CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Sep 08, 2021 @ 14:26
    Huw Reddick
    101

    Possibly this?

    var industries = item.GetPropertyValue<IEnumerable<IPublishedContent>>("industries");
    if (industries != null && industries.Any()){
        var commaList = string.Join(",", ((IEnumerable<IPublishedContent>)industries).Select(x => x.Name));
    }
    
  • Amir Khan 1289 posts 2746 karma points
    Sep 08, 2021 @ 14:33
    Amir Khan
    0

    That worked with the omission of Any(). Thank you both for your help. I'm going to try to format this for another use as a html class list like .industries-aerospace .industries-automotive but i think I can do that with some replace() statements.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies