简体   繁体   中英

How can I add certain values from one list to another list in .NET

I have created 2 lists in my code. One contains a long lists of employee's first and last names, followed by their index:

public List<Employee> FindTestEmployees()
    {
        var results = SubmitQuery($@"(Select distinct(E.Badge_Sys_No), E.Emp_Id, E.Last_Nm, E.First_Nm,e.emp_job_func_cd
                                From Employee E, Employee_Job_Func Ef, Emp_Job_Function_Group_Asgnmt Ejfga
                                Where .....");
        var EmployeeList = new List<Employee>();
        for (var i = 0; i < results.Rows.Count; i++)
        {
            var employee = new Employee();

            employee.FirstName = results.Rows[i][3].ToString();
            employee.LastName = results.Rows[i][2].ToString();
            employee.Index = i;
            EmployeeList.Add(employee);

            EmployeeList.Add(employee);
        }
        return EmployeeList;
    }

The second is a short list of employees who have reports previously generated for them. This list only contains the employee's first and last names:

public List<Employee> FindIPCREmployees()
    {
        var results = SubmitQuery($@"(Select E.first_nm, e.last_nm, Ejfga.Emp_Job_Function_Group_Cd,
                                e.emp_job_func_cd
                                From Interaction I, Interaction_Activity Ia, Appointment A, Branch B,
                                Employee E, Employee_Job_Func Ef, Emp_Job_Function_Group_Asgnmt Ejfga
                                Where .....)");


        var IPCREmployeesList = new List<Employee>();
        for (var i = 0; i < results.Rows.Count; i++)
        {
            var employee = new Employee();
            employee.FirstName = results.Rows[i][0].ToString();
            employee.LastName = results.Rows[i][1].ToString();
            IPCREmployeesList.Add(employee);
        }
        return IPCREmployeesList;
    }

What I'm trying to do is create a third list that will contain the first and last names of employees from the second list and the index of that specific employee from the 1st list. The important part being the link between the lists by the name of the employee. Is there a way of doing this in .NET? If so I would really appreciate the help.

I believe you need a Dictionary<Employee,int> :

Dictionary<Employee,int> dict=new Dictionary<Employee,int>();

foreach (var employee in IPCREmployeesList)
{
   dict.Add(employee,EmployeeList.IndexOf(employee));
}

Assuming I'm creating the final list you are looking for correctly:

var employeeList = FindTestEmployees();
var secondList = FindIPCREmployees();
var finalList = employeeList.Join(secondList, a => new {a.FirstName, a.LastName}, b => {b.FirstName, b.LastName}, (a,b) => a);

In this case finalList will be a collection of Employee objects from the first list that had entries with a matching first and last name in your second list. I recommend becoming good friends with LINQ if .NET is where you're spending your time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM