【问题标题】:Multiple Group By LINQ extension methods多个 Group By LINQ 扩展方法
【发布时间】:2018-07-18 12:31:59
【问题描述】:

我有一个 C#.NET 应用程序并希望在多个条件下进行 Group By。 我有一个这样的列表:

var testq = new List<TestQuestion>()
            {
                new TestQuestion
                {
                    Id = 1,
                    QuestionId = 1,
                    SelectedAnswerId = null
                },
                new TestQuestion
                {
                    Id = 2,
                    QuestionId = 2,
                    SelectedAnswerId = 1
                },
                new TestQuestion
                {
                    Id =3,
                    QuestionId = 1,
                    SelectedAnswerId = 1
                },
                new TestQuestion
                {
                    Id = 4,
                    QuestionId = 3,
                    SelectedAnswerId = 5
                },
                new TestQuestion
                {
                    Id = 5,
                    QuestionId = 1,
                    SelectedAnswerId = 2
                },
                new TestQuestion
                {
                    Id = 6,
                    QuestionId = 3,
                    SelectedAnswerId = 3
                },
                new TestQuestion
                {
                    Id =7,
                    QuestionId = 4,
                    SelectedAnswerId = null
                },
                new TestQuestion
                {
                    Id =8,
                    QuestionId = 5,
                    SelectedAnswerId = null
                },
            };

我的代码是:

  var result = testq
                .Where(p => p.SelectedAnswerId.HasValue)
                .GroupBy(p => p.QuestionId)
                .Select(p => p.FirstOrDefault())
                .ToList();

现在,结果 ID 是 (2 ,3, 4) 但结果不正确...

结果应该是这样的: ID -> (2 ,3, 4, 7, 8)

我想根据 QuestionID 字段按结果分组,并且第一个没有 (SelectedAnswerId) 字段值的记录为空,

此外,无论输出中的字段 (SelectedAnswerId) 的值如何,问题 ID 仅存在一次的记录。也就是说,列表中的最后两项

请指导我...

【问题讨论】:

  • 所需的输出是:您只需要一次 QuestionId,无论它是否有答案。对吗?
  • 如果有答案,应该在输出中给出第一个不为空的响应,如果分组后只有一个问题没有回答,必须在输出中看到跨度>
  • 请尝试我的回答并告诉我,

标签: c# asp.net entity-framework linq


【解决方案1】:

试试这个:

var result = testq
            .Where(p => p.SelectedAnswerId.HasValue || testq.Count(x => x.QuestionId == p.QuestionId) == 1)
            .GroupBy(p => p.QuestionId)
            .Select(p => p.FirstOrDefault())
            .Distinct()
            .ToList();

C# Fiddle

【讨论】:

  • @MohammadGhaffariasar 好消息。请将其标记为答案,请不要忘记投票 ;-)
【解决方案2】:

您必须在Select 中过滤SelectedAnswerId 而不是在Where 子句中。试试下面的,

 var result = testq
             .GroupBy(p => p.QuestionId)
             .Select(p => 
              {
                 var grouped = p.ToList(); //Get the groupBy list
                 TestQuestion testQuestion = grouped.FirstOrDefault(x => x.SelectedAnswerId.HasValue); //Get anything with AnswerId
                 return testQuestion ?? grouped.FirstOrDefault(); //If not not available with Answer then get the First or Default value
              })
              .ToList();

C# Fiddle 带有测试数据。

【讨论】:

  • 您的解决方案是正确的,但比接受的投票贵...
【解决方案3】:

虽然在 GroupBy 之后每个组中元素的顺序是相当定义的(参见Enumerable.GroupBy,但您想要的元素似乎不是每个组中的第一个。

您希望每个组的第一个元素具有非空 SelectedAnswerId,或者如果没有这样的元素,您希望每个组的第一个元素具有空 SelectedAnswerId。

这个怎么样:

var result = testQ.GroupBy(question => question.QuestionId);
    // every group contains a sequence of questions with same questionId
    // select the ones with a null SelectedAnswerId and the ones with a non-null value
    .Select(group => new
    {
        NullSelectedAnswer = group
            .Where(group.SelectedAnswerId == null)
            .FirstOrDefault(),
        NonNullselectedAnswer = group
            .Where(group.SelectedAnswerId != null)
            .FirstOrDefault(),
    })
    // if there is any NonNullSelectedAnswer, take that one, otherwise take the null one:
    .Select(selectionResult => selectionResult.NonNullSelectedAnswer ??
                               selectionResult.NullSelectedAnswer);

【讨论】:

    猜你喜欢
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多