Copied to clipboard

Flag this post as spam?

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


  • Martin 114 posts 313 karma points
    Sep 11, 2018 @ 08:00
    Martin
    0

    Merge two selections into one

    Hi, I have a problem merging two selection into one variable. I have tried using concat but I cannot figure out the correct syntax, or perhaps I should use another approach?

    Any help would be greatly appreciated :-)

            // Selection 1
            var Coll1= homePage1.Descendants("Ledig").Where("Fastid = @0", @Lokal.Fastid).FirstOrDefault();
            var image1 = Coll1.Bilds.FirstOrDefault(); 
    
            // Selection 2  
            var Coll2= homePage1.Descendants("Ledig2").Where("FastighetsId = @0", @Lokal.Fastid).FirstOrDefault();
            var image2 = Coll2.Bilds.FirstOrDefault();
    
            // MERGE SELECTIONS
    
            var image12= image1.Concat(image2);  //This does not work!
    
    //Code generating output data
    
        @{if(image12!= null)
                    {
                    // do something
                      <a href="@image12.bildfil"  data-lightbox="[email protected]@Lokal.Objektsid" data-title="@image12.Bildrubrik">
                                <!-- Use ImageGen querystring to automatically generate a thumbnail & setup fallback image -->
                                <img style="vertical-align: middle;" src="/[email protected]&height=120&width=200&crop=resize&align=center&altImage=/Media/no-image.png" alt="@image12.Name"/>
                       </a>
    
                    }
                    }
    
    
                 </div>
                }
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 11, 2018 @ 08:49
    Alex Skrypnyk
    0

    Hi Martin

    Can you show all code?

    Alex

  • Martin 114 posts 313 karma points
    Sep 11, 2018 @ 09:14
    Martin
    0

    Hi,

    I have added the code that generates html code showing the images and text. Note that my code works fine if I only use either 'image1' or 'image2'.

    For example var image12= image1; will make the code work fine (but of course I then miss the data from image2).

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Sep 11, 2018 @ 09:27
    Nik
    0

    Hi Martin,

    You are trying to merge two individual elements together, which fundamentally isn't possible because how would the system know which properties to replace etc.

    What you can do is this:

    var myCombinedList = new List<IPublishedContent>();
    myCombinedList.Add(image1);
    myCombinedList.Add(image2);
    

    Then you can do the following:

    foreach(image in myCombinedList)
    {
            <div>@image.Name</div>
    }
    

    This would output the name of both of the images one after the other.

    Does that help?

    Nik

  • Martin 114 posts 313 karma points
    Sep 11, 2018 @ 09:56
    Martin
    0

    Ok thanks you, I will try this. I let you know if it works.

  • Martin 114 posts 313 karma points
    Sep 11, 2018 @ 17:52
    Martin
    0

    It didn't work well, Trying to use myCombinedList.Add(image1); throws server error.

    One idea I have is to combine Coll1.Bilds and Coll2.Bilds before selecting FirstOrDefault().

    I should point out that Bild is a doc type and Bilds will thus contain the elements found all pages based on doc type Bild ( i.e. .[DocTypeName]s ) .

    Do you think that would make it easier?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 11, 2018 @ 09:33
    Alex Skrypnyk
    0

    Martin, are you sure that these lines are working?

    var image2 = Coll2.Bilds.FirstOrDefault();
    var image1 = Coll1.Bilds.FirstOrDefault(); 
    

    What is "Bilds"?

  • Martin 114 posts 313 karma points
    Sep 11, 2018 @ 09:57
    Martin
    0

    Hi,

    Yes it works fine. 'Bild' is a document type. I will try Nik's suggestion och hope it works.

Please Sign in or register to post replies

Write your reply to:

Draft