【问题标题】:Linq - filter a child list<object> without affecting Parent row countLinq - 在不影响父行数的情况下过滤子列表<object>
【发布时间】:2019-04-02 23:48:46
【问题描述】:

我有一个场景,我需要过滤一个子 list&lt;object&gt; ResponseIssues,它包含在父 Question 中,它也是 list&lt;object&gt;。对于这个例子,我有 10 个问题要从表中撤回,无论是否存在 ResponseIssues,我都需要撤回这些问题。

我的查询似乎存在一些问题。第一个问题是 Questions 的数量从 10 变为 1,因为我目前只有一个与 ResponseIssues 相关的问题。我需要所有问题都回来。

第二个问题是当我仔细观察 ResponseIssues 孩子list&lt;object&gt; 时。虽然我看到与问题相关的记录,但它并没有按 SuveryPeriodRespondentByQuarterId 过滤掉行。我期待一行,我得到三行,其中两行来自上一时期。 Responses 子列表也会出现同样的问题。

下面是我当前的代码。关于如何重构考虑到上述问题并返回 Questions 对象而不是匿名的查询的任何想法?

var question = await _dbContext.Questions
                 .Include(x => x.Responses)
                 .Include(x => x.ResponseIssues)
                 .Include(x => x.SurveySection)
                 .Include(x => x.Survey)
                 .Where(x => x.SurveyId == surveyId &&
                             x.Responses.Any(r => r.SiteUserId == siteUserId &&
                                                  r.SurveyPeriodId == surveyPeriodId &&
                                                  r.RespondentByQuarterId == 2
                                            ) &&
                                             x.ResponseIssues.Any(ri => ri.SurveyPeriodId == surveyPeriodId && 
                                                                        ri.RespondentByQuarterId == 2
                                           ))
                 .OrderBy(x => x.Position)
                 .ToListAsync();

【问题讨论】:

    标签: c# entity-framework linq


    【解决方案1】:

    我能够通过将其分解为三个单独的查询而不是一个单独的查询来完成上述操作。不过,我仍然很想知道社区中是否有人有办法将其作为一个查询来完成。

    无论如何,下面是我的代码。我可以使用正确的 Responses 和 ResponseIssues 行数更新 Questions Parent 并返回所有问题。

    var question = await _dbContext.Questions
                                    .Include(x => x.SurveySection)
                                    .Include(x => x.Survey)
                                    .Where(x => x.SurveyId == surveyId)
                                    .OrderBy(x => x.Position)
                                                      .ToListAsync();
    
    var responses = await _dbContext.Responses
                                    .Where(x => x.SiteUserId == siteUserId &&
                                                x.SurveyPeriodId == surveyPeriodId)
                                    .ToListAsync();
    
    var responseIssues = await _dbContext.ResponseIssues
                                    .Where(x => x.SurveyPeriodId == surveyPeriodId &&
                                                x.SiteUserId == siteUserId)
                                    .ToListAsync();
    
    
    foreach (var item in question)
    {
        var foundResponse = responses.Where(x => x.QuestionId == item.Id).ToList();
        var foundResponseIssue = responseIssues.Where(x => x.QuestionId == item.Id).ToList();
    
        if (foundResponse != null)
        {
            item.Responses = foundResponse;
        }
    
        if (foundResponseIssue != null)
        {
            item.ResponseIssues = foundResponseIssue;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多