我有一个非常大的数据表,这里是它的一个快照:Linq查询,建立嵌套的对象与一个DataTable
Section |SectionId | Lesson | LessonId | Activity | Date | Time | Score | Duration
| | | | | | | |
Functions | 123 | Intro to Functions | 6374 | Activity Quiz 1 | 6/10/2014 | 18:55:00 | 80 | 5
Functions | 123 | Intro to Functions | 6374 | Domain and Range | 6/10/2014 | 19:33:00 | 0 | 36
Functions | 123 | Intro to Functions | 6374 | Activity Quiz 2 | 6/10/2014 | 19:37:00 | 100 | 4
Linear Functions| 124 | Intro to Linear Functions| 6378 | Authentic Task: 1 | 6/23/2014 | 13:54:00 | 0 | 0
Linear Functions| 124 | Intro to Linear Functions| 6378 | Activity Quiz: 3 | 6/18/2014 | 14:12:00 | 80 | 4
Linear Functions| 124 | Intro to Linear Functions| 6378 | Step Functions | 6/18/2014 | 14:59:00 | 0 | 46
我有后续的C#类:
public class Section
{
public int Id { get; set; }
public string Name { get; set; }
public List<Lesson> Lessons { get; set; }
}
public class Lesson
{
public int Id { get; set; }
public string Name { get; set; }
public List<Activity> Activities { get; set; }
public int SectionId { get; set; }
}
public class Activity
{
public string Name { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public int Score { get; set; }
public int Duration { get; set; }
public int LessonId { get; set; }
}
我想建立嵌套JSON可以通过角被消耗掉,这样的事:
[{ Name: "Functions",
Lessons: [{ Name: "Intro to Functions",
Activities: [
{ Name: "Activity Quiz 1", Duration: 5, Date: "6/10/2014", Time: "18:55:00", Score: 80},
{Name: "Domain and Range", Duration: 36, Date: "6/10/2014", Time: "19:33:00", Score: 0},
{Name: "Activity Quiz 2", Duration: 4, Date: "6/10/2014", Time: "19:37:00", Score: 100}
]
}]
},
{ Name: "Linear Functions",
Lessons: [{ Name: "Intro To Linear Functions",
Activities: [
{Name: "Authentic Task: 1", Duration: 0, CompletedDate: "6/23/2014", CompletedTime: "13:54:00", Score: 0},
{Name: "Activity Quiz 3", Duration: 4, CompletedDate: "6/18/2014", CompletedTime: "14:12:00", Score: 80},
{Name: "Step Functions", Duration: 46, CompletedDate: "6/18/2014", CompletedTime: "14:59:00", Score: 0}
]
}]
}
]
这是我曾尝试:
var result = dt.AsEnumerable()
.GroupBy(r => new { ID = r["Section"] })
.Select(c => new Section
{
Name = c.Key.ID.ToString(),
Lessons = c.Select(l => new Lesson
{
Name = l["Lesson"].ToString(),
Activities = c.
.Select(a => new Activity
{
Name = a["Activity"].ToString(),
CompletedDate = a["Date"].ToString(),
CompletedTime = a["Time"].ToString(),
Score = Convert.ToInt32(a["Score"]),
Duration = Convert.ToInt32(a["Duration"])
}).ToList()
}).ToList()
}).ToList();
不幸的是,它没有返回正确的数据。这些部分似乎恢复得很好,但活动与课程没有正确联系,而是与部分联系在一起。所以,我想撬动IDS并将其分解成块:
var data = dt.AsEnumerable();
var sections = data
.GroupBy(r => new { ID = r["SectionId"] }).Select(c => new Chapter
{
Id = Convert.ToInt32(c.Key.ID),
Name = c.Select(n => n["Section"]).FirstOrDefault().ToString()
}).Distinct().ToList();
var lessons = data
.GroupBy(r => new { ID = r["LessonId"] }).Select(c => new Lesson
{
Id = Convert.ToInt32(c.Key.ID),
Name = c.Select(n => n["Lesson"]).FirstOrDefault().ToString()
}).Distinct().ToList();
var activities = data
.Select(l => new Activity
{
Name = l["Activity"].ToString(),
LessonId = Convert.ToInt32(l["LessonId"])
}).ToList();
的数据为每个单独的查询看起来正确,但最终我还是没能得到数据,我想它。任何帮助或建议被理解
编辑:
这里是角数据结合部分(studentData是关于范围的嵌套的对象):
<table class="table">
<thead>
<tr>
<th>Activities</th>
<th>Completed Date</th>
<th>Completed Time</th>
<th>Score</th>
<th>Status</th>
<th>Duration (hh:mm:ss)</th>
</tr>
</thead>
<tbody ng-repeat="section in studentData">
<tr>
<td><strong>{{section.Name}}</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr ng-repeat="lesson in section.Lessons">
<td>
<table>
<tr>
<td> {{lesson.Name}}</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr ng-repeat="activity in lesson.Activities">
<td>{{activity.Name}}</td>
<td>{{activity.CompletedDate}}</td>
<td>{{activity.CompletedTime}}</td>
<td>{{activity.Score}}</td>
<td>{{activity.Score}}</td>
<td>{{activity.Duration}}</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>