Copied to clipboard

Flag this post as spam?

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


  • Robin Nicholl 137 posts 277 karma points
    Dec 09, 2014 @ 16:14
    Robin Nicholl
    0

    Combine collections with Razor

    I have a multisite installation, and I want to make a collection that contains all the 'news' nodes of all the sites so that I can display news from each site on the 'master' site. Eg...

    var collection1 = CurrentPage.Descendants("NewsPageSite1").OrderBy("publishDate desc");
    var site2 = Umbraco.ContentAtRoot().Last();
    var collection2 = site2.Descendants("NewsPageSite2").OrderBy("publishDate desc");
    var comboCollection = collection1 + collection2;
    foreach( var node in comboCollection ) {
        @* dostuff *@
    }
    

    Is this possible? And, if so, how can I order the new, combined collection?

    Also, is there a way I can get the site nodes other than Umbraco.ContentAtRoot().First() and .Last() - which obviously limits me to the first and the last node?

    Thanks

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Dec 10, 2014 @ 08:52
    Alex Skrypnyk
    102

    Hi Robin,

    You can use .Concat() or .AddRange() methods for combining lists.

    http://stackoverflow.com/questions/4488054/merge-two-or-more-lists-into-one-in-c-sharp-net

    Thanks

  • Robin Nicholl 137 posts 277 karma points
    Dec 10, 2014 @ 14:14
    Robin Nicholl
    0

    Thanks, Alex – very helpful and has done the trick!

  • Robin Nicholl 137 posts 277 karma points
    Dec 10, 2014 @ 14:42
    Robin Nicholl
    0

    One more thing. I need to be able to get the various collections, ideally by somehow referencing the site nodes by name, eg:

    var collection1 = Umbraco.ContentAtRoot().First().Descendants("NewsPageSite1").Where("Visible");
    var collection2 = [ site node named eg "Site 2" ].Descendants("NewsPageSite2").Where("Visible");
    var collection3 = [ site node named eg "Site 3" ].Descendants("NewsPageSite3").Where("Visible");
    var collection4 = Umbraco.ContentAtRoot().Last().Descendants("NewsPageSite4").Where("Visible");
    

    Getting the first and last site nodes is easy enough, but how can I get the 2nd, 3rd, etc – particularly as I may want to get just 1, 2 and 4 or 1 and 3 etc.

    Thanks

  • 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