所以我有一个List<AnswerInfo>
类型的对象,其中AnswerInfo
由
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; }
}
我尝试对其进行分组,以便我可以遍历SectionTitle
,并为每个SubsectionTitle
进行迭代。
我跟随示例
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);
}
}
}
<{3>} https://msdn.microsoft.com/en-us/library/bb545974.aspx。
我拥有的是
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;
我用
循环遍历它@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>
}
我收到了错误
&#39;对象&#39;不包含&#39; Key&#39;
的定义
就行了
<td class="black-n-white" colspan="4">@sec.Key</td>
知道导致此错误的原因是什么?
或者至少,我知道如何调试它?