[英]Using LINQ, where one object property has a value of x, how do I return the value in a different property
I have an object, which I'm trying to query against instantiations of using LINQ.我有一个 object,我试图查询使用 LINQ 的实例。 Where one property of an object has a given value, I need to return the value of another property
如果 object 的一个属性具有给定值,我需要返回另一个属性的值
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任何帮助,将不胜感激
To return eg the StepCount
for all Job whose LevelNumber is 1 and JobNumberOnLevel is 2:例如,返回
StepCount
为 1 且 JobNumberOnLevel 为 2 的所有 Job 的 StepCount:
IEnumerable<int> jobStepCounts = JobList
.Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
.Select(j => j.StepCount);
BEcause there is more than one potential match, you'll get a collection of items out;因为有不止一个潜在的匹配,你会得到一组物品; you can't store multiple of those in an int
你不能在一个 int 中存储多个
If you want multiple properties, you could project a new collection of anonymous types, for example:如果你想要多个属性,你可以投影一个新的匿名类型集合,例如:
var jobStepCountAndJobNames = JobList
.Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
.Select(j => new { j.StepCount, j.JobName });
It's easier to var it than try and declare an exact type for these anonymous things.对它进行 var 比尝试为这些匿名事物声明一个确切的类型更容易。
To return the first job StepCount that matches the criteria:要返回符合条件的第一个作业 StepCount:
int jobStepCount = JobList
.FirstOrDefault(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2).StepCount;
Note that this can crash if there is no matching job;请注意,如果没有匹配的作业,这可能会崩溃; it will return a null that then crashes upon access with a null reference exception.
它将返回一个 null,然后在访问时崩溃,并出现 null 引用异常。 You can do something like:
您可以执行以下操作:
int? jobStepCount = JobList
.FirstOrDefault(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)?.StepCount;
The ?.
?.
before stepcount will not try to access stepcount on the null that is returned if no job matches.如果没有作业匹配,则在 stepcount 之前不会尝试访问 null 上的 stepcount。 It will just immediately return the null and the nullable
int?
它会立即返回 null 和可为空的
int?
will have no value将没有价值
If you don't want a single result, but instead an average, sum, or other aggregation of all the stepcounts, look at something like:如果您不想要单个结果,而是想要所有步数的平均值、总和或其他聚合,请查看以下内容:
int jobStepCountAverage = JobList
.Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
.Average(j => j.StepCount);
I dunno what property you want to trap, here i suppose you want the enumerate List of JobName:我不知道你想捕获什么属性,在这里我想你想要 JobName 的枚举列表:
var jobStepNodeCountForJob = JobList.Where(j => j.LevelNumber == 1 && j.JobNumberOnLevel ==2).Select(j => j.JobName);
You want to both select the chosen property and since it can be many matching nodes choose how to reduce them to a single number.您想要 select 选择的属性,因为它可以有许多匹配节点选择如何将它们减少到一个数字。 I think this is what you want
我想这就是你想要的
int jobStepNodeCountForJob = JobList
.Where(j => j.LevelNumber == 1 && j.JobNumberOnLevel == 2)
.Select(j => j.StepCount)
.Sum();
I'd download LINQpad or use a similar tool to get a feeling for linq.我会下载 LINQpad 或使用类似的工具来感受 linq。
public void Example()
{
// Two examples:
// 1. You want the FIRST value of some int property from your object (CustomInputCount int this example)
// Output: '2'
// Please note: When using FirstOrDefault() if no result is coming from the condition you will get a null value
int jobStepNodeCountForJob = JobList.Where(j => j.LevelNumber == 1 && j.JobNumberOnLevel == 2)
.Select(x => x.CustomInputCount).FirstOrDefault();
// 2. You want the all values of some int property from your object (CustomInputCount int this example) as a list
// Output (List<int>) -> {2,2,2,2,2,2}
List<int> jobStepNodeCountForJob_List = JobList.Where(j => j.LevelNumber == 1 && j.JobNumberOnLevel == 2)
.Select(x => x.CustomInputCount).ToList();
}
var jobExecutor= JobList.FirstOrDefault(j => j.LevelNumber == 1 && j.JobNumberOnLevel ==2)?.JobExecutor;
It will find firs Job in the list that fits the condition in brackets and assign it's JobExecutor
field to jobExecutor
variable or null if such job not found.它将在列表中找到符合括号中条件的第一个作业,并将其
JobExecutor
字段分配给jobExecutor
变量或 null 如果找不到此类作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.