Referencing a property from an object range in a for each within a nested for each

Matt F

Member
Joined
Dec 26, 2020
Messages
7
Programming Experience
Beginner
I've created a list of instances of an object, to give sample data. I've then created a list of IGrouping, containing the objects. Within the foreach, I'm then trying to give an ordering number for the objects within their respective groups (this for each only assigns a value where the value should be 1). Within the nested foreach, I want to compare a property of the current object ('subItem') to a property of the group it's within ('item'). I can't seem to find the syntax for it. The issue is the 'if' conditional, where it says '//problem here' at the end of the line.

My code is as below:

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);
                    }
                }
            }

The properties themselves are list. So for example the 2nd and 3rd objects both have the 'item' value of 2. Where the item value is the same, I want to compare the lists attached to the object. For the 2nd and 3rd items, the lists look like this:

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");

And so because 'in3' has no values found in 'out2', it should be allocated a value of 1. Conversely, because 'in2' has a value found in 'out1' it should not.

Any help would be appreciated.
 
Last edited:
C#:
var containsCommonValue = in3.Intersect(out2).Count() > 0;
 
Solution
C#:
var containsCommonValue = in3.Intersect(out2).Count() > 0;
Tut, tut.
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.
 

Latest posts

Back
Top Bottom