title: C# List Iteration Performance
body: <p>I have a for loop that does 24 total iterations each representing a single hour of the day and then checks each 15 minute interval in another nested for loop. An additional nest checks a List for the hour and minute value and then aggregates some of the items in my List if they meet my time requirement. The issue is that my List can contain up to 1 million records which means that I traverse 1 million records 24*4 times. </p>
  
  <p>How can I optimize my code for faster performance in this case? I know this could probably be simplified with LINQ statements but I'm not sure it would make it faster. Here's an example of what I am doing.</p>
  
  <pre><code>List&lt;SummaryData&gt; Aggregates = new List&lt;SummaryData&gt;();
  for(int startHour = 0; startHour &lt; 24; startHour++)
  {
     for(int startMin = 0; startMin &lt; 60; startMin+= 15)
     {
        int aggregateData = 0;
        //My ItemList can have up to 1 million records.
        foreach(ListItem item in ItemList)
        {
           if((item.time.Hour == startHour)&amp;&amp;(item.time.Minute == startMinute))
           {
              aggregateData += item.number;
           }
        }
           SummaryData aggregate = new SummaryData { SummaryId = item.id, TotalNumber = aggregateData
           Aggregates.Add(aggregate);
  
     }
  }
  class SummaryData
  {
     public int SummaryId {get; set;}
     public int TotalNumber {get; set;}
  }
  </code></pre>