-1

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
}
Simon Karlsson
  • 4,090
  • 22
  • 39
user5648283
  • 5,913
  • 4
  • 22
  • 32
  • I recommend looking at [LINQ](https://msdn.microsoft.com/en-us/library/bb397906.aspx) for your list. – mugabits Feb 04 '16 at 16:31
  • Your current model does not have any relationship to what your wanting to display in the view. Suggest you look at the answers [here](http://stackoverflow.com/questions/31128152/mvc-adding-values-of-radio-buttons-checked-into-a-collection-and-saving-to-data/31128309#31128309) and [here](http://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) to get you started –  Feb 05 '16 at 00:48

1 Answers1

1

Assuming you have the List<Answer> in a variable called list, you can use GroupBy like this:

var section_title_groups = list.GroupBy(x => x.SectionTitle);

foreach(var section_title_group in section_title_groups)
{
    var section_title = section_title_group.Key;

    var sub_section_title_groups = section_title_group.GroupBy(x => x.SubsectionTitle);

    foreach(var sub_section_title_group in sub_section_title_groups)
    {
        var sub_section_title = sub_section_title_group.Key;

        foreach(var answer in sub_section_title_group)
        {
            //Access answer here
        }

    }
}
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62