[英]Convert a List of Anonymous type to a list of a specific Class type
I have an abstract class "Employee". 我有一个抽象类“ Employee”。
I have a factory method that extends on this Employee class to query the database to return a list of all active employees:- 我有一个扩展到此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;
}
}
The var 'emp' is a list of all active employees but an anonymous list. var'emp'是所有在职员工的列表,但是匿名列表。 I want to convert this into a strongly typed List of type Employee. 我想将其转换为Employee类型的强类型列表。 I have 3 commented statements in my code which were my attempts. 我的代码中有3条带注释的语句,这是我的尝试。
What is the relationship between F_Emp and HR_EmpLon? F_Emp和HR_EmpLon是什么关系? It seems that these are loosely coupled through Employee_Id / EMPID depending on the table, but which table represents "Employee"? 根据表,这些似乎通过Employee_Id / EMPID松散耦合,但是哪个表代表“ Employee”?
Firstly: This does not look like it needs to be an extension method. 首先:这看起来好像不需要是扩展方法。 Extension methods are meant for creating a method that will apply to a given instance of a variable. 扩展方法用于创建将应用于给定变量实例的方法。 In this case "AllCitiesEmps" you are not using this instance, so at a minimum this could just be a Static method on Employee itself. 在这种情况下,“ AllCitiesEmps”没有使用此实例,因此至少这可能只是Employee自身的静态方法。 (Frankly though, better served as a Repository method) (坦率地说,最好将其用作存储库方法)
If Employee is mapped to F_Emp then the join is unnecessary: 如果将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;
}
}
If Employee maps to the HR_EmpLon table and these tables do not share a common FK between them: (Disclaimer, this is a stab from memory, so it may need some tweaking. I rarely ever need to use explicit joins.) 如果Employee映射到HR_EmpLon表,并且这些表之间没有共享公用的FK :(免责声明,这是内存的刺伤,因此可能需要进行一些调整。我很少需要使用显式联接。)
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;
}
}
If an Employee is not an entity mapped to either table, but represents a mix of data from these two tables, then I would recommend setting up a View in your database to join this data, and map your entity to the view. 如果Employee不是映射到两个表的实体,而是表示来自这两个表的数据的混合,那么我建议您在数据库中设置一个View来联接此数据,然后将您的实体映射到该视图。
You can try something like this: 您可以尝试如下操作:
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
{
}
}
Please note, you will need to create a new derived type though, as you can't directly cast to an abstract type. 请注意,您将需要创建一个新的派生类型,因为您不能直接转换为抽象类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.