lundi 20 juillet 2020

Performance issue with C# if statement

I do have below function which taking more time for execution can someone help to write it properly,its getting while importing data for validation purpose

public void MapReferences(Company company, ISession session, List<Contract> contracts, List<Employee> employees, List<CompanySettingsMap> companies, List<Contact> contacts, IList<ImportEmployeeOrgProfileBatchItem> importedOrgProfileData, int lineNum = 2)
        {
            var dateConvertor = new DateConverter();

            DateTime date;
            foreach (var item in importedOrgProfileData)
            {
                item.CompanyId = company.Id;
                item.LineNumber = lineNum;
                item.CompanyReference = company?.Reference;

                Employee employee = new Employee();
                Contact contact = new Contact();

                #region " Organisation Validation"
                if (string.IsNullOrEmpty(item.Organisation))
                {
                    item.CanContinue = false;
                    item.ErrorsList.Add("Invalid Organisation");
                }
                else
                {
                    var allcontact = contacts.Where(x => x.Company.Id == company.Id && x.Reference.ToLower() == item.Organisation.ToLower()).ToList();

                    if (allcontact.Count > 1)
                    {
                        item.ErrorsList.Add("More than one contact found matching with organisation");
                    }
                    else 
                    {
                        contact = allcontact.FirstOrDefault();
                    }

                    if (contact != null)
                    {
                        item.AgencyId = contact.Id;
                        item.AgencyReference = contact.Reference;
                    }
                    else
                    {
                        item.ErrorsList.Add("Contact not found matching with organisation");
                        item.CanContinue = false;
                    }
                }
                #endregion
                if(string.IsNullOrEmpty(item.PostCode))
                {
                    item.ErrorsList.Add("Postcode is required");
                    item.CanContinue = false;
                }
               
                #region " Employee Number Validation"
                if (string.IsNullOrEmpty(item.EmployeeNumber) || item.EmployeeNumber.Length < 8)
                {
                    item.CanContinue = false;
                    item.ErrorsList.Add("Invalid Employee Number");
                }
                else
                {
                    string employeenumber = item.EmployeeNumber.Substring(0, 8);
                    employee = employees.Where(x => !string.IsNullOrEmpty(x.ExternalReference) && x.ExternalReference.ToLower().StartsWith(employeenumber.ToLower())).FirstOrDefault();

                    if (employee != null)
                    {
                        item.EmployeeId = employee.Id;
                        item.EmployeeExternalRef = employee.ExternalReference;

                        if (company.IsPayrollEnabled && employee.PayrollGroup?.Id != null)
                        {
                            item.PayrollGroupId = employee.PayrollGroup?.Id;
                            var payrollGroup = session.Query<PayrollGroup>().Where(x => x.Id == employee.PayrollGroup.Id).FirstOrDefault();
                            if (payrollGroup != null)
                            {
                                item.Payroll = payrollGroup.Description.ToString();
                            }
                        }
                    }
                    else
                    {

                        item.StarterDeclaration = Interfaces.HMRC.StarterDeclaration.OnlyJob;
                        item.TaxCode = session.Query<PayeSetting>().Single(x => x.Year == GovernmentTaxYearEndDate(DateTime.Today).Year).DefaultTaxCode;

                        item.EmployeeExternalRef = item.EmployeeNumber;
                        item.ChangeLogList.Add("Employee " + item.EmployeeNumber + " will be created");
                        item.CanContinue = true;
                    }
                }
                #endregion

                #region " Validate Contract(Assignment number) "
                if (string.IsNullOrEmpty(item.ContractNumber))
                {
                    item.CanContinue = false;
                    item.ErrorsList.Add("Invalid Contract Number");
                }
                else
                {
                    if (dateConvertor.TryConvertFromString(item.ContractStartDateString, out date))
                    {
                        item.ContractStartDate = date;
                    }

                    if (dateConvertor.TryConvertFromString(item.ContractEndDateString, out date))
                    {
                        item.ContractEndDate = date;
                    }

                    var contract = contracts.Where(x => !string.IsNullOrEmpty(x.ContractReference) && x.ContractReference.ToLower() == item.ContractNumber.ToLower()).FirstOrDefault();

                    if (employee == null && contract != null)
                    {
                        item.ContractReference = contract.ContractReference;

                        item.ErrorsList.Add("Contract can not be created because contract with reference " + contract.ContractReference + " is already assigned to other employee");
                        item.CanContinue = false;
                    }
                    else if (contract != null)
                    {
                        if (contract.Employee.Id == employee.Id)
                        {
                            item.ContractId = contract.Id;
                            item.ContractReference = contract.ContractReference;
                        }
                        else
                        {
                            item.ErrorsList.Add("Contract with reference " + contract.ContractReference + " is already assigned to other employee");
                            item.CanContinue = false;
                        }
                    }
                    else
                    {
                        item.ContractReference = item.ContractNumber;

                        if (contact == null)
                        {
                            item.ErrorsList.Add("Contract can not be created automatically because matching contact not found");
                            item.CanContinue = false;
                        }
                        else
                        {
                            item.ChangeLogList.Add("Contract " + item.ContractNumber + " will be created");
                        }
                    }
                }
                #endregion


                #region " Parse and Assign Dates to date fields "

                if (dateConvertor.TryConvertFromString(item.StartDateInPositionString, out date))
                {
                    item.StartDateInPosition = date;
                }

                if (dateConvertor.TryConvertFromString(item.IncrementalDateString, out date))
                {
                    item.IncrementalDate = date;
                }

                if (dateConvertor.TryConvertFromString(item.FixedTermEndDateString, out date))
                {
                    item.FixedTermEndDate = date;
                }

                if (dateConvertor.TryConvertFromString(item.WtrOptOutDateString, out date))
                {
                    item.WtrOptOutDate = date;
                }

                if (dateConvertor.TryConvertFromString(item.AdjustedServiceDateString, out date))
                {
                    item.AdjustedServiceDate = date;
                }

                if (dateConvertor.TryConvertFromString(item.NHSEntryDateString, out date))
                {
                    item.NHSEntryDate = date;
                }

                if (dateConvertor.TryConvertFromString(item.BirthDateString, out date))
                {
                    item.BirthDate = date;
                }

                if (dateConvertor.TryConvertFromString(item.DateFirstHiredString, out date))
                {
                    item.DateFirstHired = date;
                }
                else if (!item.EmployeeId.HasValue)
                {
                    item.DateFirstHired = DateTime.Today.Date;
                }
                #endregion

                if (!string.IsNullOrEmpty(item.Title))
                {
                    item.Title = item.Title.Replace(".", "");
                }

                if (string.IsNullOrEmpty(item.MaritalStatus))
                {
                    item.MaritalStatus = "U";
                }

                item.Payroll = string.Empty;

                lineNum++;

                item.Errors = item.ErrorsList.Count > 0 ? String.Join("|", item.ErrorsList.Distinct()) : string.Empty;
                item.ChangeLog = item.ChangeLogList.Count > 0 ? String.Join("|", item.ChangeLogList) : string.Empty;
            }
        }

Aucun commentaire:

Enregistrer un commentaire