We have an existing SQL Server stored procedure with following query. We need to create a collection of Student
objects in the following class design, from the result of the query.
What is the best way to create the objects from the SqlDataReader
using LINQ
?
Note: I am using SqlDataReader
only; no ORM
Query
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
Class
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<ExamMark> examResults { get; set; }
}
SqlDataReader
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
}
}
REFERENCES