简体   繁体   English

C#-List <>链接ID到名称

[英]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. 我得到了一个从数据库中读取员工数据的应用程序,唯一的问题是员工的老板是在employee_id中设置的,而不是我要查找的名称。

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. 因此,我正在寻找一种通过列表通过LINQ进行查询的方法,以将employee_id链接到名字/姓氏。

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 示例:ID为1的员工是Nancy,老板是Andrew,但没有说Andrew表示2,而是应该说Andrew。

I also added 2 images below to add to my explanation. 我还在下面添加了2张图片,以补充说明。

Listview of the employees 员工名单

Reading out the list of employees into the ListView 将员工列表读入ListView

So you need to Left Join ID with Boss and get the Boss Info if found: 因此,您需要与Boss保持Left Join ID并获得Boss Info(如果找到):

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
}

First, load the employees into some local variable (you'll need it later): 首先,将员工加载到某个局部变量中(稍后将需要):

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

Then edit your foreach cycle: 然后编辑您的foreach周期:

// '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): 现在,您可以这样获得老板的姓名(在foreach周期中):

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

(You need at least C# 6.0 for the ? operator.) (对于?运算符,您至少需要C#6.0。)

you can use lambda to achieve this. 您可以使用lambda实现此目的。

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. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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