Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ,其中一个对象属性的值为x,如何在其他属性中返回该值_C#_Linq - Fatal编程技术网

C# 使用LINQ,其中一个对象属性的值为x,如何在其他属性中返回该值

C# 使用LINQ,其中一个对象属性的值为x,如何在其他属性中返回该值,c#,linq,C#,Linq,我有一个对象,我正试图对使用LINQ的实例进行查询。当一个对象的一个属性具有给定值时,我需要返回另一个属性的值 我的构造函数如下所示: public Job(string organisationType, string contractingOrganisationType, int levelNumber, string jobName, int jobNumberOnLevel, string jobExecutor, int stepCount, int customInputCoun

我有一个对象,我正试图对使用LINQ的实例进行查询。当一个对象的一个属性具有给定值时,我需要返回另一个属性的值

我的构造函数如下所示:

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));
几个模拟实例如下所示:

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

对于级别编号为1且JobNumberOnLevel为2的所有作业,如果有任何帮助,请返回例如
步数

IEnumerable<int> jobStepCounts = JobList
  .Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
  .Select(j => j.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;
int jobStepCountAverage = JobList
  .Where(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)
  .Average(j => j.StepCount);
stepcount之前的
?。
将不会尝试访问在没有作业匹配时返回的null上的stepcount。它将立即返回null,可为null的
int?
将没有值


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

int? jobStepCount = JobList
  .FirstOrDefault(j => j.LevelNumber == 1 && j => j.JobNumberOnLevel == 2)?.StepCount;
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);

您既要选择所选属性,又要选择如何将其减少为单个数,因为它可以是多个匹配节点。我想这就是你想要的

int jobStepNodeCountForJob = JobList
    .Where(j => j.LevelNumber == 1 && j.JobNumberOnLevel == 2)
    .Select(j => j.StepCount)
    .Sum();
我会下载LINQpad或使用类似的工具来感受linq。

public void Example()
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();
}
{ //两个例子: //1.希望从对象中获取某些int属性的第一个值(本例中为CustomInputCount int) //输出:“2” //请注意:当使用FirstOrDefault()时,如果条件没有结果,您将得到一个空值 int jobStepNodeCountForJob=作业列表。其中(j=>j.LevelNumber==1&&j.JobNumberOnLevel==2) .Select(x=>x.CustomInputCount).FirstOrDefault(); //2.您希望对象(本例中为CustomInputCount int)中某些int属性的所有值作为列表 //输出(列表)->{2,2,2,2,2} List jobStepNodeCountForJob\u 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。

您希望获取哪个属性?您应该指定在
中选择
,其中返回一个IEnumerable。如果查询返回单个对象或null,则应使用FirstOrDefault,然后在检查null后,可以从作业ReturnedCreat中获取所需的属性,谢谢Caius。第一个选项适用于我,因为我的设置应该只允许在where中的属性组合中使用一个“StepCount”值。但是,始终进行防御编程;)-你可能会确信,你永远不会有超过一份1级和2级的工作。。然后新员工开始把各种疯狂的数据放进去。。如果有可能有一天会出现多次,并且您至少希望通过合理的错误消息而不是潜在的基于错误内容的处理导致崩溃,那么您可以使用
Single
/
SingleOrDefault
而不是
First
Single在多个项目匹配时抛出异常,当然了,我的观点是正确的。在我上线之前,我将在那里为每次上传附加一个GUID。再次感谢Hanks Fredrik,我要去看看LINQpad。事实证明,林克很难让我清醒过来。