有没有办法摆脱DTO

有没有办法摆脱DTO

本文介绍了有没有办法摆脱DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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
其中

a.ApplicationID = = ApplicationID

选择新的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();
返回数据;
}


解决方案

如果您需要返回列表的员工,只需选择e,即指员工,不要将Employee1用作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
其中

a.ApplicationID = = ApplicationID

选择e).ToList();
返回数据;
}

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

  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();
返回数据;
}


I am using Entity Framework. I have the following query in which I get data using two tables Application and Employee connected by a foreign key EmployeeID in Application Table. The tables have 1-1 relationship .Is there a way to simplify the following code and get rid of the DTO Employee1which is the same as auto generated Employee 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;
}
解决方案

If you need to return list of Employees, just select e which refers to Employee and don't use Employee1 as a 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;
}

Another way is this, which I would prefer because of readability:

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;
}

这篇关于有没有办法摆脱DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:22