iub3rskillz
iub3rskillz

Reputation: 75

C# - List<> link id to name

I got this application that reads the employee data from the database, only problem the boss of an employee is set in employee_id and not in name, which is what I am looking for.

I got a List filled with all the employees. So I am looking for a way to query it with LINQ through the list to link the employee_id to the Firstname/Lastname.

Example: The employee with the ID 1 is Nancy and her boss is Andrew, but it doesn't say Andrew it says 2. But it should say Andrew

I also added 2 images below to add to my explanation.

Listview of the employees

Reading out the list of employees into the ListView

Upvotes: 0

Views: 2351

Answers (3)

Saket Choubey
Saket Choubey

Reputation: 916

you can use lambda to achieve this.

class Program
{
    static void Main(string[] args)
    {
        List<Employee> employees = new List<Employee>();
        employees.Add(new Employee() { EmployeeName = "Nancy", EmployeeId = 1, BossId = 2 });
        employees.Add(new Employee() { EmployeeName = "Andrew", EmployeeId = 2, BossId = 0 });
        employees.Add(new Employee() { EmployeeName = "Janet", EmployeeId = 1, BossId = 2 });

        var employeesWithBossName = employees.Join(employees,
             emp1 => emp1.BossId,
             emp2 => emp2.EmployeeId,
             (emp1, emp2) => new { EmployeeName = emp1.EmployeeName, BossName = emp2.EmployeeName });

        foreach (var item in employeesWithBossName)
        {
            Console.WriteLine("{0} {1}", item.EmployeeName, item.BossName);
        }
        Console.Read();
    }
}

public class Employee
{
    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public int BossId { get; set; }
}

Hope this will help.

Upvotes: 0

Martin Heraleck&#253;
Martin Heraleck&#253;

Reputation: 5779

First, load the employees into some local variable (you'll need it later):

List<Employee> employees = Database.getEmployees();

Then edit your foreach cycle:

// 'employee' is better because here it is really just one specific employee
foreach (Employee employee in employees)

Now you can get the name of the boss like this (in foreach cycle):

string boss = employees.FirstOrDefault(x => x.ID == employee.ReportsTo)?.FirstName;

(You need at least C# 6.0 for the ? operator.)

Upvotes: 1

Zein Makki
Zein Makki

Reputation: 30022

So you need to Left Join ID with Boss and get the Boss Info if found:

var employees = Database.getEmployees();

var employeesWithBoss = (from e in employees
                        join b in employees
                        on e.ID equals b.Boss into leftJoin
                        from boss in leftJoin.DefaultIfEmpty()
                        select new
                        {
                            Employee = e,
                            BossFirstName = boss == null ? null : boss.FirstName,
                            BossLastName = boss == null ? null : boss.LastName          
                        }).ToList();

foreach (var employee in employeesWithBoss)
{
    // do your normal work here, you now
    // have employee.BossFirstName and employee.BossLastName
}

Upvotes: 1

Related Questions