GroupBy LINQ声明

时间:2013-05-24 13:13:37

标签: c# linq group-by

我有一个按主题和父母分组的问题列表。

例如我会

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

                  });

2 个答案:

答案 0 :(得分: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 },
    };

主题ID的第一个GroupBy组。第二个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);
        }
    }
}

答案 1 :(得分:0)

我认为GroupBy实际上并不是你想要的 - 我认为你想要一个层次结构。 e.g:

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

            });