1

How do I get the last 3 items out of my list of images? I am using a foreach loop so i can get a single image and access its properties such as the Url for the img tag.

here is my code:

<div>
    @foreach (var image in Model.Images)
    {
        <img src="@image.Url" class="ArticleMainImage" />
    }
</div>
6
  • and . . . what is the question?
    – Mark
    Aug 31, 2016 at 8:40
  • Is Images IList<T> or IEnumerable<T>?
    – user3559349
    Aug 31, 2016 at 8:40
  • how do i get the last 3 items of a list of something in a foreach loop
    – Kyle B Cox
    Aug 31, 2016 at 8:41
  • Images is a List<T>
    – Kyle B Cox
    Aug 31, 2016 at 8:42
  • for(int i = Model.Images.Count - 4, i < Model.Images.Count; i++) { ... } but why are you not just sending the correct list to the view in the first place?
    – user3559349
    Aug 31, 2016 at 8:43

5 Answers 5

5
Model.Images.Reverse().Take(3).Reverse();

For Enumerable results -

 Model.Images.AsEnumerable().Reverse().Take(3).Reverse()

Found an extension method for suitability -

public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> coll, int N)
{
    return coll.Reverse().Take(N).Reverse();
}

Source - https://stackoverflow.com/a/3453282/1982631

1
  • this is bad solution. This means you take all images to memory in the first reverse, than take first 3 and then that 3 items reverse. If it is List<T> and not enumerable, much better solution is use for(int i = length-3; i < length; i++) { list[i]... } Aug 31, 2016 at 9:49
3

Here is LINQ code that should take 3 last items if collection has more than 3 items. It doesn't do any sorting to keep images order intact. It just skips several first items.

<div>
    @foreach (var image in (Model.Images.Count > 3 ? Model.Images.Skip(Model.Images.Count - 3) : Model.Images))
    {
            <img src="@image.Url" class="ArticleMainImage" />
    }
</div>
3

Since Model.Images is a List<Image> you can simply access its Count property and index the last three elements in a for loop:

@for (int i = Model.Images.Count - 3; i < Model.Images.Count; i++)
{
    <img src="@Model.Images[i].Url" class="ArticleMainImage" />
}
4
  • How do i get the properties that at inside the list such as the url
    – Kyle B Cox
    Aug 31, 2016 at 8:46
  • @KyleBC I'm not sure I understand you... doesn't Model.Images[i].Url work? Model.Images[i] gives you the single image from the list at index i.
    – René Vogt
    Aug 31, 2016 at 8:49
  • are you sure about the -4? count is not zero-based is it? so a count of 5, minus 3 = 2. i=2, which is the 3rd item, giving the 3rd, 4th and 5th.
    – wazz
    Aug 31, 2016 at 8:53
  • @wazz omg you're right....I'll fix that...of course I want Count-3, Count-2 and Count-1.
    – René Vogt
    Aug 31, 2016 at 8:55
1

If images are a List, i think you can use:

List<Image> last3 = im.OrderByDescending(i => i).Take(3).ToList();
-1

You can use for clause instead of foreach in order to control better the number of images you acquire from the list

<div>
@{
     @for(int i=0; i<= 3; i++)
     {
        if(i >= Model.Images.Count)
            break; //That in case your collection has less than 3 images
        var image = Model.Images[Model.Images.Count-i-1];
        <img src="@image.Url" class="ArticleMainImage" /> 
     } 
}
</div>
1
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. Aug 31, 2016 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.