我有一个按主题和家长分组的问题列表。
例如我会有
Subject 1
Parent 1
Question 1
Subject2
Parent2
Question2
Subject1
Parent2
Question3
Subject2
Parent2
Question4
现在我希望显示某种树视图,即类似
Subject 1
Parent 1
Question 1
Parent 2
Question 2
Question 3
Subject 2
Parent 1
Question 4
Parent 2
Question 5
Question 6
如何使用 GroupBy LINQ 语句实现这一点?
我尝试了一些还不行的东西
var questionSubjectGrps = questionsBll.GetReportQuestions()
.Where(x => x.VersionId == iLatestVersion)
.OrderByDescending(x => x.SubjectId)
.GroupBy(subject => new { subject.ReportQuestionTitle, subject.ParentId });
任何帮助将不胜感激
谢谢
** * ** * ** * ** * ** * ** * ****新代码* ** * ** * ** * ** * ** *
IEnumerable<ReportQuestion> questionSubjectGrps = questionsBll.GetReportQuestions().Where(x => x.VersionId == iLatestVersion);
var tree = questionSubjectGrps.Select(questSubGrps => questSubGrps.SubjectId)
.Distinct()
.Select(q => new
{
Subject = q.SubjectId,
Parents = questionSubjectGrps
.Where(q2 => q2.SubjectId == q.SubjectId)
.Select(q2 => new
{
Parent = q2.ParentId,
Question = questionSubjectGrps
.Where(q3 => q3.SubjectId == q.SubjectId && q3.ParentId == q2.ParentId)
.Select(q3 => q3.QuestionId)
})
});