我创建了一个对象实例的列表,以提供示例数据。然后,我创建了包含对象的IGrouping列表。在foreach中,然后尝试为对象在其各自的组中给出一个排序编号(每个对象仅分配一个值,该值应为1)。在嵌套的foreach中,我想将当前对象(“ subItem”)的属性与其所在组(“ item”)的属性进行比较。我似乎找不到它的语法。问题是“ if”条件,在行末显示“ //问题在这里”。
我的代码如下:
属性本身是列表。因此,例如,第二个和第三个对象的'item'值均为2。在item值相同的情况下,我想比较附加到该对象的列表。对于第二项和第三项,列表如下所示:
因此,由于“ in3”在“ out2”中没有找到值,因此应为其分配一个值1。相反,由于“ in2”在“ out1”中具有一个值,所以应该没有。
任何帮助,将不胜感激。
我的代码如下:
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: