Search code examples
c#htmlrazorslamdata

Want to display a maximum of 5 thumbnails


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)" />
}

Solution

  • 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