LINQ Where one property equals x, return the value of another property

Matt F

Member
Joined
Dec 26, 2020
Messages
7
Programming Experience
Beginner
I have a constructor for an object, which I'm trying to query against instantiations of using LINQ. Where one property has a given value, I need to return the value of another property

My constructor is as below:

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

A couple of mock instances look as below:

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

My query so far looks like this (though not working obviously):

int jobStepNodeCountForJob = JobList.Where(j => j.LevelNumber == 1).Where(j => j.JobNumberOnLevel == 2).Select(;

Any help would be appreciated
 
Solution
Specify which property Select(j => j.TheProperty)
The query may return multiple results and will always be IEnumerable, if you only want first result use FirstOrDefault method (First will throw exception if there are no results)
You need also only use one Where, you can add multiple expressions with && operator.
Specify which property Select(j => j.TheProperty)
The query may return multiple results and will always be IEnumerable, if you only want first result use FirstOrDefault method (First will throw exception if there are no results)
You need also only use one Where, you can add multiple expressions with && operator.
 
Solution
Thanks John. I had been playing around with Select, but it seems like I had a combination of issues stopping me from getting it working. That's it working now

var jobStepNodeCountForJob = JobList.Where(j => j.LevelNumber == ii && j.JobNumberOnLevel == kk).Select(j => j.StepCount);
 
Glad you figured it out. Also thank you for posting your code as text instead of a screenshot. Now time to level up, and learn how to post your code as text in code tags. :)
 
Back
Top Bottom