title: Creating objects from stored procedure result
body: <p>We have an existing SQL Server stored procedure with following query. We need to create a collection of <code>Student</code> objects in the following class design, from the result of the query.</p>
  
  <p>What is the best way to create the objects from the <code>SqlDataReader</code> using <code>LINQ</code>?</p>
  
  <p>Note: I am using <code>SqlDataReader</code> only; no ORM</p>
  
  <p><strong>Query</strong></p>
  
  <pre><code>SELECT 
      S.StudentID, S.StudentName, E.ExamID, E.ExamName, SE.Mark 
  FROM 
      StudentExam SE
  INNER JOIN 
      Student S ON S.StudentID = SE.StudentID
  INNER JOIN 
      Exam E ON E.ExamID = SE.ExamID 
  </code></pre>
  
  <p><strong>Class</strong></p>
  
  <pre><code>public class ExamMark
  {
      public int ExamID { get; set; }
      public string ExamName { get; set; }
      public int Mark { get; set; }
  }
  
  public class Student
  {
      public int StudentID { get; set; }
      public string StudentName { get; set; }
      public List&lt;ExamMark&gt; examResults { get; set; }
  }
  </code></pre>
  
  <p><strong>SqlDataReader</strong> </p>
  
  <pre><code>   SqlDataReader reader = command.ExecuteReader();
     if (reader.HasRows)
     {
        while (reader.Read())
        {
  
        }
     } 
  </code></pre>
  
  <p><strong>REFERENCES</strong></p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/12496809/linq-fill-objects-from-left-join?rq=1">LINQ: Fill objects from left join</a></li>
  <li><a href="https://stackoverflow.com/questions/20472106/complex-group-by-on-datatable">Complex GROUP BY on DataTable</a></li>
  </ol>