我怎样才能遍历这些元素的列表呢?

时间:2016-02-04 16:09:16

标签: c# asp.net asp.net-mvc tsql data-structures

我有[{"field1":"off"},{"field2":"on"},{"field3":"off"},{"field4":"on"},{"field5":"off"}]

类型的元素
List

当我检索它时,它将对应于从数据库中重新获取行,如

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; }
}

我通过存储过程

来检索它
-- =========================================================================================================================
--   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  

并且在C#方面,

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

现在问题在于构建我的视图我希望能够遍历我的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(); }

[伪代码]

List<AnswerInfo>

如何以这种方式重新调整for each section title for each subsection title for each { question id, question text, answer id, answer val } ?更好的是,我可以预先创建所需的数据结构,然后在迭代每个结果行时直接添加标题,如

List<AnswerInfo>

1 个答案:

答案 0 :(得分:1)

假设您在名为List<Answer>的变量中有list,您可以像这样使用GroupBy

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
        }

    }
}