2

I have this slider inside a Razor Syntax foreach loop, there are about 20 images, I only want to take 5 out of the 20 images, and randomize every set of 5 images, below is the code, any help would be appreciated.

@foreach (var data in Model.BeaconsOfHope.Take(5))
{
  <div class="swiper-slide">
    <img class="pull-left" src="@data.image" />
    <div class="pull-left donor-info">
      <span class="name" style="font-weight:bold;">@data.name</span>
      @*<span class="rank">@data.rank</span>*@
      <span class="country">@data.country</span>
    </div>
  </div>
}
7537247
  • 303
  • 5
  • 19
  • What does it mean "doesn't like "Random()" part"? In LINQ there is no Random method, is it your extension method? If not, check e.g. http://stackoverflow.com/questions/9449452/linq-order-by-random how to shuffle a collection. But I think that you want also to change the order of the methods call, because now it will take first 5 images and shuffle them. Don't you want to shuffle images first and then select first 5 images? – Lukas.Navratil Dec 04 '14 at 23:28
  • Yes, that's exactly what I want, shuffle images first and then select first 5 images. – 7537247 Dec 04 '14 at 23:45
  • 2
    Then you have to switch the method calls `Model.BeaconsOfHope.OrderBy(x => Guid.NewGuid()).Take(5)` – Lukas.Navratil Dec 04 '14 at 23:46

1 Answers1

1

Change first line to:

@foreach (var data in Model.BeaconsOfHope.orderby(x=>Guid.NewGuid()).Take(5))

Navid
  • 609
  • 6
  • 6
  • How is this different than the answer provided by Jookyn in the comments? – Techie Dec 26 '16 at 13:03
  • I want to also mention that all lowercase "orderby" threw an error for me. I followed the casing from Lukas and that worked properly for me. – Stu Furlong Aug 14 '17 at 17:30