在嵌套的每个对象中引用的每个对象范围中的属性

已加入
2020年12月26日
留言内容
7
编程经验
Beginner
我创建了一个对象实例的列表,以提供示例数据。然后,我创建了包含对象的IGrouping列表。在foreach中,然后尝试为对象在其各自的组中给出一个排序编号(每个对象仅分配一个值,该值应为1)。在嵌套的foreach中,我想将当前对象(“ subItem”)的属性与其所在组(“ item”)的属性进行比较。我似乎找不到它的语法。问题是“ if”条件,在行末显示“ //问题在这里”。

我的代码如下:

Context:
List<Job> rawJobList = new List<Job>();
            rawJobList.Add(new Job("ProjectBriefCreation", "ProjectOwner", "Owner", null, in1, out1, 1));
            rawJobList.Add(new Job("ProjectManagement", "ContractsManager", "GeneralContractor", "Owner", in2, out2, 2));
            rawJobList.Add(new Job("DesignManagement", "DesignContractsManager", "DesignContractor", "Owner", in3, out3, 2));
            rawJobList.Add(new Job("BuildingDesign", "LeadArchitect", "ArchitecturalPractice", "DesignContractor", in4, out4, 3));
            rawJobList.Add(new Job("StructuralDesign", "StructuralEngineer", "StructuralEngineeringPractice", "DesignContractor", in5, out5, 3));
            rawJobList.Add(new Job("Drywalling", "Carpenter", "Carpentry", "GeneralContractor", in6, out6, 3));
            rawJobList.Add(new Job("Plastering", "Plasterer", "PlasteringAndPainting", "GeneralContractor", in7, out7, 3));
            rawJobList.Add(new Job("Painting", "Painter", "PlasteringAndPainting", "GeneralContractor", in8, out8, 3));

            List<IGrouping<int, Job>> jobsByLevelNumber = rawJobList.GroupBy(j => j.OrganisationLevelNumber).ToList();

            List<Job> toBeDiscarded = new List<Job>();
            List<Job> alreadyAdded = new List<Job>();
            Dictionary<Job, int> allocateNumberOnLevel = new Dictionary<Job, int>();

            foreach (var item in jobsByLevelNumber)
            {
                foreach (var subItem in item)
                {
                    if (!subItem.OrderedCustomInputName1.Any() && subItem.OrganisationType != "Owner")
                    {
                        toBeDiscarded.Add(subItem);
                    }
                    if (!item.Any(i => i.OrderedCustomOutputName1 == subItem.OrderedCustomInputName1))
                    {
                        allocateNumberOnLevel.Add(subItem, 1);
                        alreadyAdded.Add(subItem);
                    }
                }
            }

属性本身是列表。因此,例如,第二个和第三个对象的'item'值均为2。在item值相同的情况下,我想比较附加到该对象的列表。对于第二项和第三项,列表如下所示:

List properties:
            List<string> in2 = new List<string>(); in2.Add("c"); in2.Add("e");
            List<string> out2 = new List<string>(); out2.Add("g");

            List<string> in3 = new List<string>(); in3.Add("c"); in3.Add("d");
            List<string> out3 = new List<string>(); out3.Add("e"); out3.Add("f");

因此,由于“ in3”在“ out2”中没有找到值,因此应为其分配一个值1。相反,由于“ in2”在“ out1”中具有一个值,所以应该没有。

任何帮助,将不胜感激。
 
Last edited:

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,501
地点
悉尼,澳大利亚
编程经验
10+
C#:
var containsCommonValue = in3.Intersect(out2).Count() > 0;
啧啧啧。
C#:
var containsCommonValue = in3.Intersect(out2).Any();
It's not just about more concise code. Any will basically stop at the first item in the list whereas Count enumerates the entire list. If the very first items in the two lists are a match then that's as far as it will look if you use Any, whereas using Count will examine both lists fully in order to get the full count that you don't actually care about.
 
最佳 底部