Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:17:39+00:00 2026-05-31T07:17:39+00:00

I’m looping through an array of items nested within another array of items. Almost

  • 0

I’m looping through an array of items nested within another array of items.

Almost always, the inner array contains 5 indexes, but there is an off chance that the feed I’m consuming will only have 4 indexes in it.

When I add the indexes to my ViewModel, if the fifth index is missing, everything blows up.

How can I prevent “Index is outside the bounds of the array” errors in this specific scenario?

private List<ImageViewModel> ConvertSmugMugModel(SmugMugGallery smugMugGallery)
{
    return smugMugGallery.Channel.Items.Select(i => new ImageViewModel
        {
            TinyImage = i.Group.Contents[0].Url, 
            ThumbnailUrl = i.Group.Contents[1].Url, 
            SmallImageUrl = i.Group.Contents[2].Url,
            MediumImageUrl = i.Group.Contents[3].Url,
            LargeImageUrl = i.Group.Contents[4].Url
        }).ToList();
}

*note: I’m not opposed to moving to an iCollection or some other object if using an array is not the best fit. I just didn’t want to add bloat if I didn’t need to.


Edit

Kal asked what the the model looks like in a comment below.

[XmlRoot("rss")]
public class SmugMugGallery
{
    [XmlElement("channel")]
    public m_Channel Channel { get; set; }
    public class m_Channel
    {
        [XmlElement("item")]
        public Item[] Items { get; set; }
        public class Item
        {
            [XmlElement("group", Namespace = "http://search.yahoo.com/mrss/")]
            public m_Group Group { get; set; }
            public class m_Group
            {
                [XmlElement("content", Namespace = "http://search.yahoo.com/mrss/")]
                public Content[] Contents { get; set; }
                public class Content
                {
                    [XmlAttribute("url")]
                    public string Url { get; set; }
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:17:41+00:00Added an answer on May 31, 2026 at 7:17 am

    My first thought is just use the ? operator, like so:

        private List<ImageViewModel> ConvertSmugMugModel(SmugMugModel smugMugGallery)
        {
            return smugMugGallery.Channel.Items.Select(i => new ImageViewModel
            {
                TinyImage = i.Group.Contents[0].Url,
                ThumbnailUrl = i.Group.Contents[1].Url,
                SmallImageUrl = i.Group.Contents[2].Url,
                MediumImageUrl = i.Group.Contents[3].Url,
                LargeImageUrl = i.Group.Contents.Count() > 4 ? i.Group.Contents[4].Url:new Url("blank")
            }).ToList();
        }
    

    You may also want to put that check into the rest of them also, unless you’re 101% sure you won’t run into less than 4 elements. Thusly:

        private List<ImageViewModel> ConvertSmugMugModel(SmugMugModel smugMugGallery)
        {
            return smugMugGallery.Channel.Items.Select(i => new ImageViewModel
            {
                TinyImage = i.Group.Contents.Count() > 0 ? i.Group.Contents[0].Url:new Url("blank"),
                ThumbnailUrl = i.Group.Contents.Count() > 1 ? i.Group.Contents[1].Url:new Url("blank"),
                SmallImageUrl = i.Group.Contents.Count() > 2 ? i.Group.Contents[2].Url:new Url("blank"),
                MediumImageUrl = i.Group.Contents.Count() > 3 ? i.Group.Contents[3].Url:new Url("blank"),
                LargeImageUrl = i.Group.Contents.Count() > 4 ? i.Group.Contents[4].Url:new Url("blank")
            }).ToList();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.