Linq查询联接两个列表之间

时间:2019-04-16 22:01:32

标签: c# .net entity-framework linq

首先,我有一个从AD解析dll获取的AD用户的列表。这有2个我感兴趣的属性:电话号码和电子邮件ID。 (我们称之为var EmployeeAD)

第二,我有一个用户列表,这些用户具有全名,员工ID,用户名等属性(不包含电话号码和电子邮件ID)。

    List<Employee> employees = new List<Employee>();
    using (var ctx = new hr_employeeEntities())
    {
        var emps = (from x in ctx.F_Emp
                    join y in ctx.HR_EMPL_VW on (x.employee_id).ToString() equals y.EMPLID
                    where x.employment_status == "A"
                    select new{ x, y }).ToList();

        //PSFT_Employee is an inherited class of the abstract class Employee //
        employees = emps.Select(x => (new PSFT_Employee
        {
            employee_id = x.x.employee_id,
            full_name = x.x.employee_name,
            username = x.y.OPRID,
            date_of_birth = x.x.date_of_birth,                                       
            date_of_hire = x.x.original_hire_date,

            phone = ,
            email = x.x. ,
            }) as Employee).ToList();
         }

    return employees;

我需要能够将第一个AD用户列表(基于密钥-OPRID)与第二个emps列表结合在一起...这样我就可以为每个员工填充电话和电子邮件地址。< / p>

类似:-

emps.Foreach( // for each employee in emps, join with user in employeeAD based on common OPRID. Then extract phone# and email for that user and populate in emps list//) 

2 个答案:

答案 0 :(得分:0)

Not sure if I have the requirement right, but are you looking for a second join?

List<Employee> employees = new List<Employee>();
using (var ctx = new hr_employeeEntities())
{
    var emps = (from x in ctx.F_Emp
                join y in ctx.HR_EMPL_VW on (x.employee_id).ToString() equals y.EMPLID
                where x.employment_status == "A"
                select new{ x, y }).ToList();

    //PSFT_Employee is an inherited class of the abstract class Employee //
    employees = emps.Join(
        users,//don't know what you have for this
        emp => emp.OPRID,
        user => user.OPRID,

        (u, e) => new { u, e })     
    .Select(a => (new PSFT_Employee
    {
        employee_id = a.u.x.employee_id,
        full_name = a.u.x.employee_name,
        username = a.u.y.OPRID,
        date_of_birth = a.u.x.date_of_birth,
        date_of_hire = a.u.x.original_hire_date,
        phone = a.e.phone,
        email = a.e.email,
        }) as Employee).ToList();
     }

return employees;

答案 1 :(得分:0)

尽管您没有这么说,但我假设每个EmployeeAd都有一个标识ID。因此,您的EmployeeAd如下所示:

class EmployeeAd
{
    public int Id {get; set;} // primary key
    public string PhoneNr {get; set;}
    public string Email {get; set;}
    ...
}

属性的实际类型并不重要。重要的是Id的属性类型等于第二个列表中Emplyee Id的属性类型。

如果您知道每个员工都有一个EmployeeAd,则可以使用简单的(内部)联接。如果您不希望员工没有EployeeAds,也可以使用此方法:

var joinedEmployess = dbConext.Employees.Join(  // join Employees
    dbContext.EmployeeAdds,                     // with EmployeeAds
    employee => employee.Id,                    // from every Employee take the Id
    employeeAd => employeeAd.Id,                // from every EmployeeAd take the Id

    (employee, employeeAd) => new PSFT_Employee() // when they match make one new
    {
        // fill the Employee properties you plan to use
        Id = employee.Id,
        Name = employee.Name,
        ...

        // fill the PhoneNr and Email:
        PhoneNr = employeeAd.PhoneNr,
        Email = employeeAd.Email,
    });