0

Want to show max 5 images in a thumbnails. But It print all images it have.

@foreach (var orderline in order.OrderLines.DistinctBy(ol => ol.imageURL))
{
    <img height="40" src="@orderline.imageURL" alt="@(orderline.Listing.Title.Length > 15 ? orderline.Listing.Title.Substring(0, 15) : orderline.Listing.Title)" />
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
S.Rusdana
  • 249
  • 1
  • 2
  • 12

2 Answers2

5

You are already using some Linq in your code, so just use a little more. The Take() method in Linq will do exactly what you want:

@foreach (var orderline in order.OrderLines.DistinctBy(ol => ol.imageURL).Take(5))
{
    <img height="40" src="@orderline.imageURL" alt="@(orderline.Listing.Title.Length > 15 ? orderline.Listing.Title.Substring(0, 15) : orderline.Listing.Title)" />
}

The Take(5) I added at the end means you will only iterate through a maximum of 5 items

maccettura
  • 10,514
  • 3
  • 28
  • 35
-1

change your foreach to a for loop

@for(var i = 0; i < 5; i++)

  Your code goes here using thing[i]
Prodigle
  • 1,757
  • 12
  • 23
  • Then they would have to materialize the collection which might not be what they want to do. There are ways to do this _without_ materializing the collection. – maccettura Aug 15 '18 at 15:30