So I have an object of type List<AnswerInfo> where AnswerInfo is defined by

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

and I'm trying to group it so that I can iterate through the SectionTitles, and for each of those, the SubsectionTitles.

I'm following the example

var queryNestedGroups =
    from student in students
    group student by student.Year into newGroup1
    from newGroup2 in
        (from student in newGroup1
         group student by student.LastName)
    group newGroup2 by newGroup1.Key;

// Three nested foreach loops are required to iterate 
// over all elements of a grouped group. Hover the mouse 
// cursor over the iteration variables to see their actual type.
foreach (var outerGroup in queryNestedGroups)
{
    Console.WriteLine("DataClass.Student Level = {0}", outerGroup.Key);
    foreach (var innerGroup in outerGroup)
    {
        Console.WriteLine("\tNames that begin with: {0}", innerGroup.Key);
        foreach (var innerGroupElement in innerGroup)
        {
            Console.WriteLine("\t\t{0} {1}", innerGroupElement.LastName, innerGroupElement.FirstName);
        }
    }
}

on https://msdn.microsoft.com/en-us/library/bb545974.aspx.

What I have is

        ViewBag.GroupedAnswers = from ans in AllAnswers
                                 group ans by ans.SectionTitle into g1
                                 from g2 in (from ans in g1
                                             group ans by ans.SubsectionTitle)
                                 group g2 by g1.Key;

and I'm looping through it with

@foreach ( var sec in ViewBag.GroupedAnswers )
{
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <table class="table table-striped assessment-table">
            <tbody>
                <tr>
                    <td class="black-n-white" colspan="4">@sec.Key</td>
                </tr>
                <tr>
                    <td>Section</td><td>Component</td><td>Completeness Level (0-100%)</td><td>Readines</td>
                </tr>

                @foreach ( var subsec in sec )
                {
                    foreach ( var inf in subsec )
                    {
                        <tr>
                            <td>@subsec.Key</td>
                            <td data-qstnid="@inf.QuestionId">@inf.QuestionText</td>
                            <td data-answid="@inf.AnswerId"><input type="text" class="completeness" value="@inf.AnswerVal"/></td>
                            <td><div class="readiness-circle"></div></td>
                        </tr>
                    }

                }
            </tbody>
        </table>
    </div>
</div>
}

I'm getting the error

'object' does not contain a definition for 'Key'

on the line

<td class="black-n-white" colspan="4">@sec.Key</td>

Any idea what is causing this error?

Or at the very least, any idea how I can debug it?

Related posts

Recent Viewed