简体   繁体   English

比较两个对象C#的列表计数

[英]Comparing list counts with of two objects C#

I have been sitting with a little problem that Was hoping for some help with. 我一直在遇到一个小问题,希望能有所帮助。

I have two lists of modules that looks as follow. 我有两个模块列表,如下所示。

What I am basically trying to achieve is track completed topics. 我基本上想要实现的是跟踪已完成的主题。

m1 would represent the completed set of modules with its skills and topics, and m2 would represent topics modules, skills and topics that where accessed and that are incomplete. m1代表具有其技能和主题的完整的模块集,而m2代表访问且不完整的主题模块,技能和主题。

List <ModuleA> m1;
List <ModuleB> m2;
public class ModuleA
{
    public bool completed { get; set; }
    public string ModuleCode { get; set; }
    public List<Skill> lstKeySkills { get; set; }
}
public class ModuleB
{

    public string ModuleCode { get; set; }
    public List<Skill> lstKeySkills { get; set; }
}
public class Skill
{
    public string KeySkillName { get; set; }
    public List<Topic> LstTopics { get; set; }
}
public class Topic
{
    public string TopicName { get; set; }
}

How can I do topic count comparisons within the skills of the two modules? 如何在两个模块的技能范围内进行主题计数比较?

I have tried something like this but it seems very messy and incorrect. 我已经尝试过类似的方法,但是看起来很混乱而且不正确。

foreach (var mc in m1)
{
    foreach (var v in m2)
    {
        if (v.ModuleCode == mc.ModuleCode)
        {
            foreach (var skillsIC in v.lstKeySkills)
            {
                foreach (var skillsC in mc.lstKeySkills)
                {
                    if (skillsIC.LstTopics.Count() == skillsC.LstTopics.Count())
                    {
                        //Count is the same
                    }
                }
            }               
        }
    }
}

You can use join clause: 您可以使用join子句:

var result = from a in m1 // ModuleA iterable
             join b in m2 // ModuleB iterable
                 on new { a.ModuleCode, a.Skills.Count }
             equals new { b.ModuleCode, b.Skills.Count }
             select new { a, b };

This is a join, where the list counts match. 这是一个联接,列表计数匹配。 Its much easier in LINQ 在LINQ中更容易

var result = m1.Join(m2, a => a.ModuleCode, b => b.ModuleCode, (a,b) => new { A = a, B = b})
           .Where (x => x.A.lstKeySkills.Count() == x.B.lstKeySkills.Count());

In this example, result will be an enumerable of anonymous items, which has 2 properties 在此示例中, result将是一个匿名项目的枚举,该项目具有2个属性

  • A is the ModuleA AModuleA
  • B is the ModuleB BModuleB

A and B will always match on ModuleCode and have the same number of Skills AB将始终在ModuleCode匹配,并且具有相同的技能数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM