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>
Mark
  • 2,041
  • 2
  • 18
  • 35
Kyle B Cox
  • 421
  • 6
  • 18

5 Answers5

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

Community
  • 1
  • 1
Manoz
  • 6,507
  • 13
  • 68
  • 114
  • 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 and not enumerable, much better solution is use `for(int i = length-3; i < length; i++) { list[i]... }` – Jakub Čermoch Aug 31 '16 at 09:49
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" />
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • How do i get the properties that at inside the list such as the **url** – Kyle B Cox Aug 31 '16 at 08: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 '16 at 08: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 '16 at 08: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 '16 at 08:55
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>
dlxeon
  • 1,932
  • 1
  • 11
  • 14
1

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

List<Image> last3 = im.OrderByDescending(i => i).Take(3).ToList();
S. Medina
  • 76
  • 1
  • 8
-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>
jambonick
  • 716
  • 1
  • 6
  • 12
  • 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. – Brian Tompsett - 汤莱恩 Aug 31 '16 at 09:43