Iterating through a list based on the value of an Id element inside the list in c#
<p>I have another question that's connected with my class:</p>

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

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

}
</code></pre>

<p>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</p>

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

<p>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. </p>

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

<pre><code>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
</code></pre>

<p>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. </p>

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

<p><em>I will watch for comments so please just send a comment if there is something that's not clear.</em> </p>

<p>Thank you.</p>
