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 6048493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:28:09+00:00 2026-05-23T07:28:09+00:00

I have another question that’s connected with my class: public class Parent { public

  • 0

I have another question that’s connected with my class:

public class Parent {
        public IList<ParentDetail> ParentDetails {
            get { return _ParentDetails; }
        }
        private List<ParentDetail> _ParentDetails = new List<ParentDetail>();
        public Parent() {
            this._ParentDetails = new List<ParentDetail>();
        }
    }

    public class ParentDetail {
        public int Id { get; set; }
    }

}

Presently I iterate through ParentDetails and do some action for each ParentDetail. I am using MVC razor syntax so that’s the reason for the @( .. etc

@foreach (int index in Enumerable.Range(0, Model.Parent.ParentDetails.Count()))
{
  Model.Parent.ParentDetails[@index].Id

This gives me the numbers 1,2,3 … etc IF the Ids have been assigned sequentially in the list with the first element having an Id of 1 and the second element having an Id of 2 etc.

However the Ids in my list are not assigned sequentially. I may have it like this:

list-element [0] has an Id of 3
list-element [1] has an Id of 4
list-element [2] has an Id of 1
list-element [3] has an Id of 2

What I need is something equivalent to the foreach that will let me iterate through my list based on the value of Id. Maybe I need to use the Index method to find but I don’t know how to put this all into a loop.

I know some C# but this is quite far beyond me. Can anyone help out with some ideas. I hope my question makes sense.

I will watch for comments so please just send a comment if there is something that’s not clear.

Thank you.

  • 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-23T07:28:10+00:00Added an answer on May 23, 2026 at 7:28 am

    LINQ is the super easy solution for such cases:

    foreach(var details in Model.Parent.ParentDetails.OrderBy(d => d.Id))
    {
        // details are processed in increasing order of Id here
    }
    

    You will need to add the System.Linq namespace with @using System.Linq if it’s not already included in your view.

    Aside

    Your existing code does things in a kind of roundabout way though. This:

    @foreach (int index in Enumerable.Range(0, Model.Parent.ParentDetails.Count()))
    {
        var x = Model.Parent.ParentDetails[@index].Id;
    }
    

    can also be written as

    @foreach (var details in Model.Parent.ParentDetails)
    {
        var x = details.Id;
    }
    

    which is the natural way of iterating over a collection, and far more preferable (unless you also need the index of each ParentDetails object in the collection).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another basic Rails question: I have a database table that needs to contain references
good fellas. I have another very basic question that I need help with. I
I have another SQL/access 2007 question that seems really basic but I'm not sure
I have a feeling someone is going to point me to another question that
I have another question for you. I have a python class with a list
I have another very puzzling question that has cropped up after my upgrade from
I would have another question I cannot find answer on. I know that the
Aloha everyone, I have another question. I have a SYDI script that retrieves WMI
As described in another question , I have a set of Model objects and
this kind of follows on from another question of mine. Basically, once I have

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.