繁体   English   中英

使用 LINQ,其中一个 object 属性的值为 x,我如何在不同的属性中返回该值

[英]Using LINQ, where one object property has a value of x, how do I return the value in a different property

我有一个 object,我试图查询使用 LINQ 的实例。 如果 object 的一个属性具有给定值,我需要返回另一个属性的值

我的构造函数如下:

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

几个模拟实例如下所示:

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

到目前为止,我的查询看起来像这样(尽管显然不起作用):

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

任何帮助,将不胜感激

例如,返回StepCount为 1 且 JobNumberOnLevel 为 2 的所有 Job 的 StepCount:

IEnumerable<int> jobStepCounts = JobList
  .Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
  .Select(j => j.StepCount);

因为有不止一个潜在的匹配,你会得到一组物品; 你不能在一个 int 中存储多个

如果你想要多个属性,你可以投影一个新的匿名类型集合,例如:

var jobStepCountAndJobNames = JobList
  .Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
  .Select(j => new { j.StepCount, j.JobName });

对它进行 var 比尝试为这些匿名事物声明一个确切的类型更容易。


要返回符合条件的第一个作业 StepCount:

int jobStepCount = JobList
  .FirstOrDefault(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2).StepCount;

请注意,如果没有匹配的作业,这可能会崩溃; 它将返回一个 null,然后在访问时崩溃,并出现 null 引用异常。 您可以执行以下操作:

int? jobStepCount = JobList
  .FirstOrDefault(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)?.StepCount;

?. 如果没有作业匹配,则在 stepcount 之前不会尝试访问 null 上的 stepcount。 它会立即返回 null 和可为空的int? 将没有价值


如果您不想要单个结果,而是想要所有步数的平均值、总和或其他聚合,请查看以下内容:

int jobStepCountAverage = JobList
  .Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
  .Average(j => j.StepCount);

我不知道你想捕获什么属性,在这里我想你想要 JobName 的枚举列表:

var jobStepNodeCountForJob = JobList.Where(j => j.LevelNumber == 1 && j.JobNumberOnLevel ==2).Select(j => j.JobName);

您想要 select 选择的属性,因为它可以有许多匹配节点选择如何将它们减少到一个数字。 我想这就是你想要的

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

我会下载 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;

它将在列表中找到符合括号中条件的第一个作业,并将其JobExecutor字段分配给jobExecutor变量或 null 如果找不到此类作业。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM