代码之家  ›  专栏  ›  技术社区  ›  Grizzly

使用LINQ、GroupBy更改输出样式

  •  -1
  • Grizzly  · 技术社区  · 7 年前

    我有这个 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、3包含问题3-在哪里

    如何实现此输出?

    2 回复  |  直到 7 年前
        1
  •  3
  •   Jonathon Chase    7 年前

    我不太确定方法语法是什么,但这里有一个查询语法,可以为您提供所需的输出。我认为您在尝试关闭方法语法时遇到的问题可能是由于使用 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}");
    
        2
  •  0
  •   Adam Simon    7 年前

    使用查询语法:

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

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