根据列表中的元素对同一对象进行分组

我有以下类型的对象列表Application,它看起来像:


public class Application

    {

        public int AppId { get; set; }

        public List<Question> Questions { get; set; }

    }

我想要做的是将它们映射到类型的对象,Section如下所示:


public class Section

       {

            public List<int> AppIds { get; set; }

            public List<Question> Questions { get; set; }

        }

通过根据以下规则对它们进行分组:在包含这些问题的 AppId 列表下收集所有相同的问题。示例性输入和输出:


Input: A1(Q1,Q2,Q3), A2(Q1,Q2), A3(Q1,Q3), A4(Q1). 

Output: A1,A2,A3,A4(Q1), A1,A2(Q2), A1,A3(Q3)

在LINQ中可以做到吗?还是我必须自己写逻辑?


莫回无
浏览 187回答 2
2回答

四季花海

也许有更简单的方法,但这是LINQ我写的第一个查询,至少应该足以让您入门。首先,我将您的问题和 appid 弄平,然后我按问题重新分组并按问题列出了您的 appid。请注意,您必须先填充您List的应用程序,然后才能运行。List<Application> app = new List<Application>();var output = (from a in app.SelectMany(p => p.Questions.Select(z => new {z, p.AppId})group a.AppId by new&nbsp;{a.Questions} into combinedselect new&nbsp;{combined.Key.Questions,combined.ToList()});

临摹微笑

我认为这个问题被描绘得有点过分。每个应用程序都应该有一个不同的AppId,所以基本上没有什么可以分组的,因为所有的AppIds 都是不同的。相反,我们应该按问题分组,因为每个应用程序都可能有相同的问题。这是我所拥有的(就样式而言,它与确切的输出不匹配,因为您的示例输出基于按AppId而不是问题分组,但预期结果相同):using System;using System.Collections.Generic;using System.Linq;public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // create Questions&nbsp; &nbsp; &nbsp; &nbsp; var questionOne = new Question(){QuestionId = 1, ActualQuestion = "Who"};&nbsp; &nbsp; &nbsp; &nbsp; var questionTwo = new Question(){QuestionId = 2, ActualQuestion = "What"};&nbsp; &nbsp; &nbsp; &nbsp; var questionThree = new Question(){QuestionId = 3, ActualQuestion = "Where"};&nbsp; &nbsp; &nbsp; &nbsp; // Assign questions to the specified list of questions&nbsp; &nbsp; &nbsp; &nbsp; var applicationOneQuestions = new List<Question>(){questionOne, questionTwo, questionThree};&nbsp; &nbsp; &nbsp; &nbsp; var applicationTwoQuestions = new List<Question>() {questionOne, questionTwo};&nbsp; &nbsp; &nbsp; &nbsp; var applicationThreeQuestions = new List<Question>() {questionOne, questionThree};&nbsp; &nbsp; &nbsp; &nbsp; var applicationFourQuestions = new List<Question>() {questionOne};&nbsp; &nbsp; &nbsp; &nbsp; // Create Applications&nbsp; &nbsp; &nbsp; &nbsp; var applicationOne = new Application(){AppId = 1, Questions = applicationOneQuestions};&nbsp; &nbsp; &nbsp; &nbsp; var applicationTwo = new Application(){AppId = 2, Questions = applicationTwoQuestions};&nbsp; &nbsp; &nbsp; &nbsp; var applicationThree = new Application() {AppId = 3, Questions = applicationThreeQuestions};&nbsp; &nbsp; &nbsp; &nbsp; var applicationFour = new Application() {AppId = 4, Questions = applicationFourQuestions};&nbsp; &nbsp; &nbsp; &nbsp; // Create List of Applications&nbsp; &nbsp; &nbsp; &nbsp; var lstApplications = new List<Application>(){applicationOne, applicationTwo, applicationThree, applicationFour};&nbsp; &nbsp; &nbsp; &nbsp; // Group Applications based on Questions and cast to Section Object&nbsp; &nbsp; &nbsp; &nbsp; var groupApplications = lstApplications.GroupBy(x => x.Questions).Select(t => new Section { AppIds = t.Select(z => z.AppId).ToList() , Questions = t.Key}).ToList();&nbsp; &nbsp; &nbsp; &nbsp; foreach(var item in groupApplications)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(var appId in item.AppIds)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(appId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(var question in item.Questions)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(question.ActualQuestion);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class Application&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public int AppId { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public List<Question> Questions { get; set; }&nbsp; &nbsp; }public class Section&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public List<int> AppIds { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public List<Question> Questions { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; }public class Question{&nbsp; &nbsp; public int QuestionId {get;set;}&nbsp; &nbsp; public string ActualQuestion {get;set;}&nbsp;}输出// 1// Who// What// Where// 2// Who// What// 3// Who// Where// 4// Who申请 1、2、3、4 都包含问题一应用程序 1、2 包含问题二应用程序 1、3 包含问题三请让我知道这可不可以帮你。我还为您创建了一个DotNetFiddle,供您自行测试并提供反馈。
打开App,查看更多内容
随时随地看视频慕课网APP