【问题标题】:How to query with Linq a List that contains a list of lists that contains a list of lists that contains a list of objects?如何使用 Linq 查询包含列表列表的列表,该列表包含包含对象列表的列表列表?
【发布时间】:2020-04-04 11:57:28
【问题描述】:

我需要一个包含列表列表的列表,其中包含列表列表,包含对象列表。我相信我们可以使用 Linq 实现这一目标。但我不知道怎么做!

我在这里留下了一张图表,以便更好地了解我需要什么。

https://i.gyazo.com/fe52e851024b0b13e6d39eeb533c43f2.png

我有一个对象:

public class Question
{
    public int ModuleID    { get; set; }
    public int GroupID     { get; set; }
    public int QuestionID     { get; set; }
}

我返回的列表可能如下所示:

List<Question> questionList = new List<Question>();
QuestionList.Add( new Question{ ModuleID = 1, GroupID = 1, QuestionID = 1 } );
QuestionList.Add( new Question{ ModuleID = 2, GroupID = 1, QuestionID = 2 } );
QuestionList.Add( new Question{ ModuleID = 3, GroupID = 2, QuestionID = 3 } );
QuestionList.Add( new Question{ ModuleID = 4, GroupID = 4, QuestionID = 4 } );

我尝试过的:

var groupedCustomerList = userList
    .GroupBy(u => u.GroupID)
    .Select(grp => grp.ToList())
    .ToList();

发件人:

Using Linq to group a list of objects into a new grouped list of list of objects

我已经试过了:

var groupedCustomerList = CustomerList.GroupBy(u => u.GroupID)
            .Select(grp =>new { GroupID =grp.Key, CustomerList = grp.ToList()})
            .ToList();

发件人:

Using LINQ to group a list of objects

【问题讨论】:

    标签: c# list linq


    【解决方案1】:

    这是一个小型控制台应用程序,我认为它演示了如何获得您所追求的分层分组:

    internal class Program
    {
        public class Module
        {
            public int ModuleID { get; set; }
            public List<Group> Groups { get; set; }
        }
    
        public class Group
        {
            public int GroupID { get; set; }
            public int ModuleID { get; set; }
            public List<Question> Questions { get; set; }
        }
    
        public class Question
        {
            public int ModuleID { get; set; }
            public int GroupID { get; set; }
            public int QuestionID { get; set; }
        }
    
        private static void Main(string[] args)
        {
            var questions = new List<Question>();
            questions.Add(new Question {ModuleID = 1, GroupID = 1, QuestionID = 1});
            questions.Add(new Question {ModuleID = 2, GroupID = 1, QuestionID = 2});
            questions.Add(new Question {ModuleID = 3, GroupID = 2, QuestionID = 3});
            questions.Add(new Question {ModuleID = 4, GroupID = 4, QuestionID = 4});
    
            var groups = questions
                .GroupBy(q => new {q.GroupID, q.ModuleID})
                .Select(qg => new Group
                {
                    GroupID = qg.Key.GroupID,
                    ModuleID = qg.Key.ModuleID,
                    Questions = qg.ToList()
                })
                .GroupBy(g => g.ModuleID)
                .Select(gg => new Module
                {
                    ModuleID = gg.Key,
                    Groups = gg.ToList()
                })
                .ToList();
        }
    }
    

    这里最重要的一行可能是.GroupBy(q =&gt; new {q.GroupID, q.ModuleID})。按GroupId 分组是不够的,因为我们还需要考虑问题是否在不同的模块中。例如在上述测试数据的示例中,这会通过错误地将问题 #1 和 #2 组合在一起而浮出水面,因为它们都具有相同的GroupID,而忽略了它们位于不同模块中的事实。 为了解决这个问题,我们创建了一个新的匿名对象,包含问题的GroupIDModuleID (new {q.GroupID, q.ModuleID})),并改为在该匿名对象上分组。

    【讨论】:

    • 嘿! “模块”和“组”类可以转换为一个通用类吗?如果是这样,您将如何实施?
    【解决方案2】:

    让你输入正确。您的列表有整数,但该类有字符串。使用 IEquatable

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Question> questionList = new List<Question>();
                questionList.Add(new Question { ModuleID = "1", GroupID = "1", QuestionID = 1 });
                questionList.Add(new Question { ModuleID = "2", GroupID = "1", QuestionID = 2 });
                questionList.Add(new Question { ModuleID = "3", GroupID = "2", QuestionID = 3 });
                questionList.Add(new Question { ModuleID = "4", GroupID = "4", QuestionID = 4 });
                questionList.Add(new Question { ModuleID = "4", GroupID = "4", QuestionID = 4 });
    
                List<List<Question>> results = questionList.GroupBy(x => x).Select(x => x.ToList()).ToList();
            }
        }
    
        public class Question : IEquatable<Question>
        {
            public string ModuleID { get; set; }
            public string GroupID { get; set; }
            public int QuestionID { get; set; }
    
            public Boolean Equals(Question other)
            {
                return
                    (ModuleID == other.ModuleID) &&
                    (GroupID == other.GroupID) &&
                    (QuestionID == other.QuestionID);
            }
            public override int GetHashCode()
            {
                return (ModuleID + "^" + GroupID + "^" + QuestionID.ToString()).GetHashCode();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多