0

我有一个按主题和家长分组的问题列表。

例如我会有

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

                  });
4

2 回答 2

1

您可以使用以下几个级别来执行此操作GroupBy

此输入与您提供的预期输出匹配,而不是您提供的输入。

var questions = new List<Question>
    {
        new Question { SubjectId = 1, ParentId = 1, QuestionId = 1 },
        new Question { SubjectId = 1, ParentId = 2, QuestionId = 3 },
        new Question { SubjectId = 2, ParentId = 2, QuestionId = 2 },
        new Question { SubjectId = 2, ParentId = 2, QuestionId = 3 },
        new Question { SubjectId = 2, ParentId = 1, QuestionId = 4 },
        new Question { SubjectId = 2, ParentId = 2, QuestionId = 5 },
        new Question { SubjectId = 2, ParentId = 2, QuestionId = 6 },
    };

第一个GroupBy按主题 ID 分组。第二个GroupBy按父 ID 对每个主题组进行分组。

var grouped = questions.GroupBy(
    q => q.SubjectId,
    (sid, qs) => new { SubjectId = sid, Groups = qs.GroupBy(q => q.ParentId) });

对于两个级别,GroupBy您需要三个foreach循环才能获得所有问题。

foreach (var subjectGroup in grouped)
{
    Console.WriteLine("Subject {0}", subjectGroup.SubjectId);
    foreach (var parentGroup in subjectGroup.Groups)
    {
        Console.WriteLine("\tParent {0}", parentGroup.Key);
        foreach (var question in parentGroup)
        {
            Console.WriteLine("\t\tQuestion {0}", question.QuestionId);
        }
    }
}
于 2013-05-28T09:28:12.133 回答
0

我不认为 GroupBy 实际上是你想要的 - 我认为你想要一个层次结构。例如:

  var questionSubjectGrps = questionsBll
            .GetReportQuestions()
            .Tolist();

  var tree = questionSubjectGrps.Select(questionSubjectGrps => q.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)
                            })

            });
于 2013-05-24T13:26:48.687 回答