0

Still a beginner so please bare with them me on this. I would like the ability to add a number value to my property after the list distinguishes that there are duplicate values.

For example, two users have inputted into the system that they have the same employee ID of 001, the List allows them both to go in then applies a condition to check the list for any duplicates so in this case there would be. Now this conditional is true, add 1 to that property so the last employee now has employee ID of 002.

I believe LINQ will more than likely be involved in this process, but not to sure on how to go about this. I understand I will also have to update my List after performing this after to maintain the properties state throughout the program.

Thanks in advance hope everything is clear, question again How to add value to a property within a List<>

Code Snippet below.

Employee = new Employee(employeeFirstName, employeeLastName,001); // Hard coded for sake of example. 

EmployeeList.AddEmployee(Employee);

EmployeeList Class

public static void AddEmployee(Employee employee)
{

   employees.Add(new Employee(employee.FirstName,employee.LastName,employee.EmployeeID)); 
   employees.Add(new Employee("John", "Jones", 001));    

}

public void employeeIdValidation()
{
  if (employees.Count() != employees.Select(x => new {staffId = x.EmployeeID }).Distinct().Count())
  {
     Console.WriteLine("Every Book and Category should be unique");
     // employee ID increments by 1 
     // update List

  }
  else
  {
    Console.WriteLine("No duplicate found");
  }
}

public static List<Employee> GetEmployeeList()
{
  return employees; // With the updated EmployeeID 
}
2
  • 1
    What are you using as a backend to store the data? Normally, IDs are not supplied by the users, but the database itself when the record gets inserted. Commented Dec 21, 2014 at 3:20
  • Its just being stored within the list, Yeah I completely agree with you however within this program it will be provided by the user themselves. Commented Dec 21, 2014 at 11:32

2 Answers 2

2

IMHO, you should use a Dictionary<int, Employee> (where the key is the Id) instead of a list. This will force you to update the ID of a duplicate record before adding to the collection and save a whole bunch of trouble and computation.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, If I continue to struggle with the list I have here I'll definitely use a Dictionary to move forward, as it does seem quite logical.
It's also for performance, looking up a record via key is much faster in a dictionary.
1

To find in your list what Employees have the same Id you can use this linq function.

var result=employees.ToLookup(e => e.Id);

At this way you can group the employees that have the same Id. Then, you can iterate for each group of employees with the same id as I show you below:

 foreach (IGrouping<int, Employee> group in result)
 {
     //More than 2 employees with the same Id
     if (group.Count()>1)
     {
       foreach (Employee employee in group)
       {
           //Change your ids here
       }
     }         
 }

[Update 1]: To change the Ids I proporse you find first the max id before iterate in the groups.

  int nextID = result.Max(e => e.Key)+1;

Then, change the second foreach cycle for a for cycle as you can see below. When you find a group with more than two Employees with the same id, don't change the first element, change the rest at this way:

//The first element don't change the id, start for the second element
for (int j = 1; j < group.Count(); j++)
{
   var currentEmployee=group.ElementAt(j);
   // Change the id and refresh the nextID variable
   currentEmployee.Id = nextID++;
}

2 Comments

Thanks that's perfect, but how would I then change the ID property, I've tried the following, int value = 1; employee.EmployeeID + value; .. obviously with no luck, if you could point me in a direction that would be great :) thanks for in advance
No other words than, your the man! I was going to head down the route of using the get Enumerator and calling the last method to change only the last property on the object(hope that makes sense still learning the correct dialogue)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.