将匿名类型列表转换为特定类类型的列表

时间:2019-04-15 22:12:48

标签: c# entity-framework linq

我有一个抽象类“ Employee”。

我在此Employee类上扩展了一个工厂方法,以查询数据库以返回所有活动雇员的列表:-

 public static class EmployeeExtentions
    {
        public static List<Employee> FireEmployees(this List<Employee> AllCitiesEmps)
        {
            List<Employee> employees = new List<Employee>();

            using (var ctx = new hr_employeeEntities())
            {
                var emp = (from x in ctx.F_Emp
                           join y in ctx.HR_EMPLon (x.employee_id).ToString() equals y.EMPLID
                           where x.employment_status == "A"
                           select new { x, y }).ToList();

                // emp.ForEach( x => { employees.Add(new Employee(employees)); });
                //var emps = emp.Select(x => PropertyCopy<Employee>.CopyFrom(x)).ToList();    
                //emp.ForEach(x => { employees.Add(new Employee(x)); });

            }
            return employees;
        }
    }

var'emp'是所有在职员工的列表,但是匿名列表。我想将其转换为Employee类型的强类型列表。我的代码中有3条带注释的语句,这是我的尝试。

2 个答案:

答案 0 :(得分:2)

F_Emp和HR_EmpLon之间是什么关系?似乎这些表通过Employee_Id / EMPID松散耦合,但是哪个表代表“ Employee”?

首先:这看起来好像不需要作为扩展方法。扩展方法用于创建将应用于给定变量实例的方法。在这种情况下,“ AllCitiesEmps”没有使用此实例,因此至少这可能只是Employee自身的静态方法。 (坦率地说,最好将其用作存储库方法)

如果将Employee映射到F_Emp,则不需要联接:

public static List<Employee> FireEmployees()
{
    using (var context = new hr_employeeEntities())
    {
        var employees = context.F_Emp
            .Where(x => x.employment_status == "A")
            .ToList();
        return employees;
    }
}

如果Employee映射到HR_EmpLon表,并且这些表之间没有共享公共FK :(免责声明,这是内存的刺伤,因此可能需要进行一些调整。我很少需要使用显式联接。)< / p>

public static List<Employee> FireEmployees()
{
    using (var context = new hr_employeeEntities())
    {
        var employees = context.HR_EMPLon
            .Join(context.F_Emp, 
                h => h.EMPLID, 
                e => e.employee_id.ToString(),
                (h, e) => new {HREmployee = h, FEmployee = e})
            .Where(x => x.FEmployee.employment_status == "A")
            .Select(x => x.HREmployee)
            .ToList();
        return employees;
    }
}

如果Employee不是映射到两个表的实体,而是表示来自这两个表的数据的混合,那么我建议您在数据库中设置一个View来联接此数据,然后将您的实体映射到该视图。

答案 1 :(得分:0)

您可以尝试以下操作:

public static class EmployeeExtentions
{
    public static List<Employee> FireEmployees(this List<Employee> AllCitiesEmps)
    {
        List<Employee> employees = new List<Employee>();

        using (var ctx = new hr_employeeEntities())
        {
            var emp = (from x in ctx.F_Emp
                       join y in ctx.HR_EMPL on (x.employee_id).ToString() equals y.EMPLID
                       where x.employment_status == "A"
                       select new
                       {
                           x.employee_id,
                           x.employment_status,
                           //x.OtherProperties
                           y.EMPLID,
                           //y.OtherProperties
                       }).ToList();

            employees = emp.Select(x => (new EmployeeDerived { EmployeeId = x.employee_id, EmploymentStatus = x.employment_status }) as Employee).ToList();

        }
        return employees;
    }

    private class EmployeeDerived : Employee
    {

    }    
}

请注意,您将需要创建一个新的派生类型,因为您不能直接转换为抽象类型。

相关问题