有没有办法摆脱DTO

is there a way to get rid of DTO

我正在使用实体框架。我有以下查询,其中我使用通过外键 EmployeeID[=16] 连接的两个表 ApplicationEmployee 获取数据=] 在应用程序 Table 中。这些表具有 1-1 关系。 有没有办法简化以下代码并摆脱 DTO Employee1 这与自动生成的员工相同 class

public List<Employee1> GetApplicant(int ApplicationID)
{
    var context = new FPSDB_newEntities();       
    var data = (from a in context.Applications
                join e in context.Employees on a.EmployeeID equals e.EmployeeID
                where
                (
                   a.ApplicationID == ApplicationID
                )
                select new Employee1
                {
                    EmployeeID = e.EmployeeID,
                    SecondEmail = e.SecondEmail,
                    EmailID = e.EmailID,
                    Title = e.Title,
                    Name = e.Name,
                    Rank = e.Rank,
                    POBox = e.POBox,
                    Phone = e.Phone,
                    JoinDate = e.JoinDate,
                    Status = e.Status,
                    DepartmentID = e.DepartmentID.Value,
                    NameString = e.NameString,
                    Department = e.Department,
                    ParentDept = e.ParentDept,
                    DepartmentAr = e.DepartmentAr,
                    NameAr = e.NameAr,
                    NameStringAr = e.NameStringAr,
                    TitleAr = e.TitleAr
                }).ToList();
    return data;
}

如果您需要 return 员工列表,只需 select 引用员工的 e,不要将员工 1 用作 DTO。

public List<Employee> GetApplicant(int ApplicationID)
{
    var context = new FPSDB_newEntities();       
    var data = (from a in context.Applications
                join e in context.Employees on a.EmployeeID equals e.EmployeeID
                where
                (
                   a.ApplicationID == ApplicationID
                )
                select e).ToList();
    return data;
}

另一种方式是这样,我更喜欢这种方式,因为可读性:

public List<Employee> GetApplicant(int ApplicationID)
{
    var context = new FPSDB_newEntities();       
    var data = context.Applications.Where(p=>p.ApplicationID == ApplicationID).Select(p=>p.Employee).ToList();
    return data;
}