簡體   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