I have a List
of elements of type
public class AnswerInfo
{
public string SectionTitle { get; set; }
public string SubsectionTitle { get; set; }
public int QuestionId { get; set; }
public string QuestionText { get; set; }
public int? AnswerId { get; set; }
public int? AnswerVal { get; set; }
}
which when I retrive it will correspond to retriving rows from a database like
-- =========================================================================================================================
-- section_title | subsection_title | question_id | question_text | answer_id | answer_val
-- =========================================================================================================================
-- 'Partner Planning' | 'Target Markets' | 1 | 'Have you defined the target ...?' | 1 | 24
-- 'Partner Planning' | 'Target Markets' | 2 | 'Have you identified the .......?' | 2 | 50
-- 'Partner Planning' | 'Target Markets' | 3 | 'Have you built a market .......?' | 3 | 90
-- 'Partner Planning' | 'Target Markets' | 4 | 'Have you built the revenue ....?' | NULL | NULL
-- 'Partner Planning' | 'Target Customers' | 5 | 'Have you defined the ideal ....?' | NULL | NULL
-- . . . . . .
-- . . . . . .
-- . . . . . .
-- . . . . . .
-- 'Partner Growth' | 'Getting Traction' | 61 | 'Have you defined the ideal ....?' | NULL | NULL
I retrive it by means of the stored procedure
CREATE PROCEDURE GetAnswersByPartner
@pid UNIQUEIDENTIFIER -- Partner id
AS
BEGIN
SELECT Sections.title AS section_title,
Subsections.title AS subsection_title,
Questions.id AS question_id,
Questions.qtext AS question_text,
Answers.id AS answer_id,
Answers.val AS answer_val
FROM Partners
INNER JOIN Questions ON 1 = 1
INNER JOIN Subsections ON Subsections.Id = Questions.subsection_id
INNER JOIN Sections ON Sections.Id = Subsections.section_id
LEFT JOIN Answers ON Answers.partner_id = Partners.Id
AND Answers.question_id = Questions.Id
WHERE Partners.Id = @pid
END
and on the C# side of things,
List<AnswerInfo> Answers = new List<AnswerInfo>();
using ( SqlCommand cmd = new SqlCommand("GetAnswersByPartner", this._Conn) )
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pid", pid);
this._Conn.Open();
using ( SqlDataReader dataReader = cmd.ExecuteReader() )
{
while ( dataReader.Read() )
{
Answers.Add(
new AnswerInfo
{
SectionTitle = dataReader.GetString(0),
SubsectionTitle = dataReader.GetString(1),
QuestionId = dataReader.GetInt32(2),
QuestionText = dataReader.GetString(3),
AnswerId = dataReader.GetInt32(4),
AnswerVal = dataReader.GetInt32(5)
}
);
}
}
this._Conn.Close();
}
Now the issue is that to construct my View I'd like to be able to iterate through my List<AnswerInfo>
like
[pseudo-code]
for each section title
for each subsection title
for each { question id, question text, answer id, answer val }
How can I rejig my List<AnswerInfo>
that way? Better yet, can I create the needed data structure upfront and then directly add the titles to it in when I iterate through each resulting row like
while ( dataReader.Read() )
{
// Add info to better-organized structure
}