Resolved Adding objects with a matching property to a list

Matt F

Member
Joined
Dec 26, 2020
Messages
7
Programming Experience
Beginner
I have a list of objects, each with various properties, built from a constructor, as below:

1608986084219.png


I am trying to form a list of lists, so that where the 'LevelNumber' matches it will create a list which will be added to a list according to unique 'LevelNumber' ordered by number. I think what's below must be somewhere close to what I need:

List<Job> JobList = new List<Job>();
JobList.Add(new Job("Owner", null, 0, "ProjectBriefCreation", 1, "ProjectOwner", 5, 2, 2));
JobList.Add(new Job("Carpentry", "GeneralContractor", 2, "Drywalling", 1, "Carpenter", 5, 2, 2));
JobList.Add(new Job("PlasteringAndPainting", "GeneralContractor", 2, "Plastering", 2, "Plasterer", 6, 2, 2));
JobList.Add(new Job("PlasteringAndPainting", "GeneralContractor", 2, "Painting", 3, "Painter", 8, 2, 2));
JobList.Add(new Job("GeneralContractor", "Owner", 1, "ProjectManagement", 1, "ContractsManager", 7, 2, 2));
JobList.Add(new Job("DesignContractor", "Owner", 1, "DesignManagement", 1, "DesignContractsManager", 7, 2, 2));
JobList.Add(new Job("ArchitecturalPractice", "DesignContractor", 1, "BuildingDesign", 1, "LeadArchitect", 7, 2, 2));
JobList.Add(new Job("StructuralEngineeringPractice", "DesignContractor", 1, "StructuralDesign", 1, "StructuralEngineer", 7, 2, 2));

List<List<Job>> jobsPerLevelList = new List<List<Job>>();
List<int> distinctLevelNumber = new List<int>();
distinctLevelNumber.AddRange(JobList.Select(x => x.LevelNumber).Distinct().ToList());
foreach (var Level in distinctLevelNumber)
{
if (Job.LevelNumber.)
{
jobsPerLevelList.Add(new List<Job>(Job.LevelNumber));
}

}

I can't quite seem to get it finished/working. Any advice would be appreciated.
 
Last edited:
Solution
Linq grouping seems what you should research. If you had posted code instead of image it would be easier to give an example.

Post code like this:
insertcode.png
Linq grouping seems what you should research. If you had posted code instead of image it would be easier to give an example.

Post code like this:
insertcode.png
 
Solution
Thanks John. I don't have any experience using Link. The code is as below if you have a minute to show how an example might look:
C#:
public class Job
    {
        public Guid Record { get; set; }
        public string JobName { get; set; }
        public string JobExecutor { get; set; }
        public string OrganisationType { get; set; }
        public string ContractingOrganisationType { get; set; }
        public string CustomInput { get; set; }
        public int StepNumber { get; set; }
        public string StepName { get; set; }
        public int StepCount { get; set; }
        public int CustomInputCount { get; set; }
        public int CustomOutputCount { get; set; }
        public int LevelNumber { get; set; }
        public int JobNumberOnLevel { get; set; }
        public string GenericInputType { get; set; }
        public string GenericInputDescription { get; set; }
        public string CustomOutput { get; set; }

        public Job(string organisationType, string contractingOrganisationType, int levelNumber, string jobName, int jobNumberOnLevel, string jobExecutor, int stepCount, int customInputCount, int customOutputCount)
        {
            OrganisationType = organisationType;
            ContractingOrganisationType = contractingOrganisationType;
            LevelNumber = levelNumber;
            JobName = jobName;
            JobNumberOnLevel = jobNumberOnLevel;
            JobExecutor = jobExecutor;
            StepCount = stepCount;
            CustomInputCount = customInputCount;
            CustomOutputCount = customOutputCount;
        }
Thanks,
Matt
 
Last edited by a moderator:
But you do have experience with LINQ. You used it in the code you were presenting in post #1:
JobList.Select(x => x.LevelNumber).Distinct().ToList()
 
That's right, try to do that with OrderBy and GroupBy.
 
Back
Top Bottom