按空问题划分的LINQ组

按空问题划分的LINQ组,linq,entity-framework-core,Linq,Entity Framework Core,我正在处理LINQ查询,我需要所有问题,每个问题可能有子问题,也可能没有子问题 由于某些父问题没有子问题,因此我将按空/异常问题分组。我在做左撇子;按家长提问分组 (from question in Context.Questions.Where(question => question.ConsultationId == ConsultationId) join questionHierarchy in Context.QuestionHierarc

我正在处理LINQ查询,我需要所有问题,每个问题可能有子问题,也可能没有子问题

由于某些父问题没有子问题,因此我将按空/异常问题分组。我在做左撇子;按家长提问分组

(from question in Context.Questions.Where(question => question.ConsultationId == ConsultationId)
                   join questionHierarchy in Context.QuestionHierarchy on question.Id equals questionHierarchy.ParentQuestionId into qs
                   from childQuestion in qs.DefaultIfEmpty()
                   group childQuestion by question into g
                   select new
                   {
                       g.Key,
                       g
                   }).ToList();
找到了答案

(from question in Context.Questions.Where(question => question.ConsultationId == ConsultationId)
                   join questionHierarchy in Context.QuestionHierarchy on question.Id equals questionHierarchy.ParentQuestionId into qs
                   from childQuestion in qs.DefaultIfEmpty()
                   group childQuestion by question into groupQuestions
                   select new
                   {
                       groupQuestions.Key,
                       childQuestions = groupQuestions.DefaultIfEmpty() == null? null : groupQuestions
                   }).ToList();

如果您想要“带子问题的问题”之类的内容,请使用cnsider,而不是后跟GroupBy的内部联接

小步骤:

IQueryable<Question> questions = dbContext.Questions.Where(...)
IQueryable<QuestinoHierarch> subQuestions = Context.QuestionHierarchy;

var questionsWithTheirSubQuestions = questions.GroupJoin(  // GroupJoin the questions
    subQuestions,                                          // with the subQuestions
    question => question.Id,           // from every question take the Id
    subQuestion => subQuestion.ParentQuestionId, 
                                       // from every subQuestion take the ParentQuestionId,
    (question, subQuestions) => new    // take the question with all matching subQuestions
    {                                  // to make one new object:
        // select only the question items you plan to use
        Title = question.Title,
        Priority = question.Priority,
        ...

        // select only the subQuestions that you want (or all)
        // for example only the answered sub questions:
        SubQuestions = subQuestions
             .Where(subQuestion.Type = QuestionType.Answered)
             .Select(subQuestion => new
             {
                  // again: select only the subQuestion properties you plan to use
                  Name = subQuestion.Name,
                  Date = subQuestion.Date,
                  ...
             })
             .ToList(),
    });
IQueryable questions=dbContext.questions.Where(…)
IQueryable子问题=Context.QuestionHierarchy;
var questionsWithTheirSubQuestions=questions.GroupJoin(//GroupJoin the questions
子问题,//带子问题
question=>question.Id,//从每个问题中获取Id
subQuestion=>subQuestion.ParentQuestionId,
//从每个子问题中获取ParentQuestionId,
(问题,子问题)=>新建//将问题与所有匹配的子问题一起回答
{//要创建一个新对象,请执行以下操作:
//仅选择您计划使用的问题项
标题=问题。标题,
优先级=问题,优先级,
...
//仅选择所需的子问题(或全部)
//例如,仅回答子问题:
子问题=子问题
.Where(subQuestion.Type=QuestionType.Answered)
.选择(子问题=>新建)
{
//再次:仅选择您计划使用的子问题属性
Name=子问题。Name,
日期=子问题。日期,
...
})
.ToList(),
});
TODO:如果需要,做一个大声明