我有一個非常大的數據表,這裏是它的一個快照: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>