Getting employee details according to the role Here is my issue.I have two classes:

public class Role
{
  public string RoleTitle { get; set; }
  public string Office { get; set; }
  public string Department { get; set; }
}


And

public class Employe
{
  public string Name { get; set; }
  public List&lt;Role&gt; Roles { get; set; }
}


As you can see, class Employee contains ListRoles as a property.I have one method that process some share point list and returns one Employee object with all of his roles, something like this:

John Johnson


Regular Office1 Department2
Regular Office2 Department1
Manager Office1 Department3
OfficeDirector Office1 Departmnet4 etc.


As you can see one employee can have lot of different roles and lot of same roles but for different office/department (OK, I know that this is not real life situation, I'm just using this as an example). Now I'm using this Role information to process another SP List to get more information that I need to display.Roles are very important, because queries are based on them, what employee can see or not.Also, office and department are crucial information because rules for roles are also based on them.

My current code looks like this, I'm going through foreach loop and for each role, which I determine on RoleTitle, I create query, which will be added to final query (using OR).

public void GetEmployeeInformation(Employee emp)
{
    query = string.Empty;
    List&lt;string&gt; queries = new List&lt;string&gt;();
    foreach (Role role in emp.Roles)
    {
        switch (role.RoleTitle)
        {
            case ("Regular"):
                {
                    query = CamlQuery(role.Office, role.Department, "Office", "Department", "Choice");
                    break;
                    }

                case ("Manager"):
                    {
                        query = CamlQuery("Department", role.Department);
                        break;
                    }
                case ("OfficeDirector"):
                    {
                        query = CamlQuery("Office", role.Offices);
                        break;
                    }
                case ("Admin"):
                    {
                        query = CamlQuery("some implementation")
                        break;
                    }
            }
            queries.Add(query);
        }
//further implementation of method

    }


Everything is working fine, I got right results, but I'm wondering is there any chance that I can optimize code, because, for example, if I have 5 regular roles for each one it will go through the loop.

Another idea I got is to make changes in Role class, and create ListOffices, and ListDepartments so when I query in first method, I will get, for each different role, something like this

Manager:


Office1 Department1
Office2 Department2
Office3 Department3


Now, I will have only one Role object for every role with all office/department information inside, and I can go through foreach and inside Camlquery use one recursive method that will process all offices/departments for that role and create final query.The recursive method I've created is based on indexes of elements inside the lists, because when I create query I know that for first Manager role office/department pair is on [0] position in the list.But I've been told that using indexes in situation like this are very bad practice, so I've been stucked with this idea.

Do you have any idea how can I optimize this code by avoiding going through foreach loop for every single role and not using this approach with Lists and indexes?
You can  use linq GroupBy and Select to reduce your list to distinct RoleTitle's in your foreach loop:

foreach(Role role in emp.Roles.GroupBy(r=&gt; r.RoleTitle).Select(g =&gt; g.First()))
{
    //...
}