使用LINQ,GroupBy更改输出样式

时间:2018-08-15 16:44:28

标签: c# linq group-by

我有这个DotNetFiddle

代码:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {       
        // create Questions

        var questionOne = new Question(){QuestionId = 1, ActualQuestion = "Who"};
        var questionTwo = new Question(){QuestionId = 2, ActualQuestion = "What"};
        var questionThree = new Question(){QuestionId = 3, ActualQuestion = "Where"};

        // Assign questions to the specified list of questions

        var applicationOneQuestions = new List<Question>(){questionOne, questionTwo, questionThree};
        var applicationTwoQuestions = new List<Question>() {questionOne, questionTwo};
        var applicationThreeQuestions = new List<Question>() {questionOne, questionThree};
        var applicationFourQuestions = new List<Question>() {questionOne};

        // Create Applications

        var applicationOne = new Application(){AppId = 1, Questions = applicationOneQuestions};
        var applicationTwo = new Application(){AppId = 2, Questions = applicationTwoQuestions};
        var applicationThree = new Application() {AppId = 3, Questions = applicationThreeQuestions};
        var applicationFour = new Application() {AppId = 4, Questions = applicationFourQuestions};

        // Create List of Applications

        var lstApplications = new List<Application>(){applicationOne, applicationTwo, applicationThree, applicationFour};

        // Group Applications based on Questions

        var groupApplications = lstApplications.GroupBy(x => x.Questions).Select(t => new Section { AppIds = t.Select(z => z.AppId).ToList() , Questions = t.Key}).ToList();

        foreach(var item in groupApplications)
        {
            foreach(var appId in item.AppIds)
            {
                Console.WriteLine(appId);

            }

            foreach(var question in item.Questions)
            {
                Console.WriteLine(question.ActualQuestion);
            }

            Console.WriteLine("\n");
        }
    }
}

public class Application
    {
        public int AppId { get; set; }
        public List<Question> Questions { get; set; }
    }

public class Section
       {
            public List<int> AppIds { get; set; }
            public List<Question> Questions { get; set; }
        }

public class Question
{
    public int QuestionId {get;set;}
    public string ActualQuestion {get;set;} 
}

运行程序后,将得到如下输出:

1
Who
What
Where


2
Who
What


3
Who
Where


4
Who

现在,我想将输出更改为如下形式:

A1, A2, A3, A4 - Who
A1, A2 - What
A1, A3 - Where

基本上,

  • 应用1,2,3,4都包含问题1-谁
  • 应用程序1、2包含问题2-什么
  • 应用程序1、3包含问题3-哪里

如何实现此输出?

2 个答案:

答案 0 :(得分:3)

我不太确定方法的语法是什么,但是这是一个查询语法,它将为您提供所需的输出。我认为,尝试降低方法语法时遇到的麻烦可能是由于在要使用Select的地方使用了SelectMany

var questionGroups = from application in lstApplications
                     from question in application.Questions
                     group application.AppId by question.ActualQuestion;


foreach (var question in questionGroups)
    Console.WriteLine($"{string.Join(" ", question.Select(x => $"A{x}"))} - {question.Key}");

答案 1 :(得分:0)

具有查询语法:

var appGroups =
  from app in lstApplications
  from question in app.Questions
  group app by question;

使用lambda语法:

var appGroups = lstApplications
    .SelectMany(app => app.Questions, (app, question) => new { app, question })
    .GroupBy(item => item.question, item => item.app);

打印输出:

foreach (var appGroup in appGroups)
    Console.WriteLine($"{string.Join(", ", appGroup.Select(app => "A" + app.AppId))} - {appGroup.Key.ActualQuestion}");