C# InvalidOperationException: 序列包含多个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15021257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:49:13  来源:igfitidea点击:

InvalidOperationException: Sequence contains more than one element

c#visual-studio-2010

提问by Kinyanjui Kamau

I have the following code below for a payroll program. The first dictionary holds the employee IDs and corresponding basic pays held in a master data table. The second dictionary holds the employee IDs and corresponding basic pays held in a salary fitment table - used for processing. I want to update the salary fitment basic pays for each employee ID that do not match in the master table. (Changes in salary).

我有以下工资单程序的代码。第一个字典包含主数据表中的员工 ID 和相应的基本工资。第二个字典保存了员工 ID 和相应的基本工资,保存在工资调整表中 - 用于处理。我想为每个在主表中不匹配的员工 ID 更新工资配置基本工资。(工资变化)。

var OHEMDictionary = employees.OrderBy(es => es.empID)
                     .ToDictionary(od => od.empID,
                     od => od.salary);

var SalaryFitmentDictionary = salaryFitments
                              .Where(x => x.U_PD_Code.Trim().ToString() == "SYS001")
                              .OrderBy(es => es.U_Employee_ID)
                              .ToDictionary(od => od.U_Employee_ID,
                                            od => od.U_PD_Amount);

var difference = OHEMDictionary
                .Where(kv => SalaryFitmentDictionary[kv.Key] != kv.Value);

difference.ToList().ForEach(x =>
                    {
                        decimal salary = x.Value.Value;

                        var codeToUpdate = salaryFitments
                                        .Where(y => y.U_Employee_ID.Equals(x.Key))
                                        .Select(z => z.Code)
                                        .SingleOrDefault(); `**<---exception thrown here**`

                        var salaryFitment = salaryFitmentService
                                            .GetSalaryFitment(codeToUpdate);

                        if (salaryFitment != null)
                        {
                            // Save record
                            salaryFitmentService
                           .UpdateSalaryFitment(salaryFitment, salary.ToString());
                        }
                    });

However, I keep getting the error 'Sequence contains more than one element'. How do I solve this error?

但是,我不断收到错误消息'Sequence contains more than one element'。我该如何解决这个错误?

采纳答案by Freeman

You can use FirstOrDefault() but SingleOrDefault throws an exception if more than one element exists.

您可以使用 FirstOrDefault() 但如果存在多个元素,则 SingleOrDefault 会引发异常。

Here you can see exactly what the single or default method does: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.singleordefault(v=vs.100).aspx

在这里您可以确切地看到单一或默认方法的作用:http: //msdn.microsoft.com/en-us/library/system.linq.enumerable.singleordefault(v=vs.100).aspx