如何将一类类型的集合转换为实体框架中的另一类类型集合

问题描述:

我正在使用Web Api,我将不得不创建用于在应用程序的UI上显示数据的数据传输对象。如何将一类类型的集合转换为实体框架中的另一类类型集合

我与代码第一种方法在这里工作是我的域类

public class Employee 
    { 

     [Key] 
     public int BusinessEntityId { get; set; } 


     [Required] 
     [MaxLength(50)] 

     public string JobTitle { get; set; } 

     [Required] 
     [DataType(DataType.DateTime)] 
     public DateTime BirthDate { get; set; } 


     [Required] 
     [MaxLength(1)] 
     public string MaritalStatus { get; set; } 

     [Required] 
     [MaxLength(1)] 
     public string Gender { get; set; } 

     [Required] 
     [DataType(DataType.DateTime)] 
     public DateTime HireDate { get; set; } 

     [Required] 
     public Boolean SalariedFlag { get; set; } 

     public ICollection<EmployeePayHistory> PayHistories { get; set; } 

    } 

这里是我的数据传输对象(DTO)类

public class EmployeePayHistoryListDTO 
    { 

     public int Id { get; set; } 

     public DateTime RateChangeDate { get; set; } 

     public Decimal Rate { get; set; } 

     public Int16 PayFrequency { get; set; } 

     public String JobTitle { get; set; } 

     public String Gendre { get; set; } 

    } 

现在由于PayHistories是集合在我的领域类的我我正在做的是我正在创建一个新班级 其中有收集我的DTO班级类型EmployeePayHistoryListDTO

public class EmployeeRelatedCollections 
    { 
     public ICollection<EmployeePayHistoryListDTO> PayHistories { get; set; } 
    } 

所以从我的仓库,我正确地通过以下EF声明

_context.Employees.Include("PayHistories") 
            .Include("PayHistories")         
            .Single(e=>e.BusinessEntityId==id); 

但我在哪里将我的Employee类(区域类)的集合,我DTO类型的集合有我在这里得到错误的获取数据代码

PayHistories = (from ph in employee.PayHistories 
          select new EmployeePayHistoryListDTO 
          { 
           Id = ph.BusinessEntityId, 
           RateChangeDate = ph.RateChangeDate, 
           Rate = ph.Rate, 
           PayFrequency = ph.PayFrequency, 
           JobTitle = ph.Employee.JobTitle, 
           Gendre = ph.Employee.Gender 
          }).ToList(); 


      I am getting following exception below is summary 

Execption getting

System.NullReferenceException , 
Additional Information: Object reference Not set to an instance of an object. 

Troubleshooting tips 
1. Check to determine if the object is null before calling the method, 
2. Use new keyword to create an object instance. 
+1

我想你错过了你的错误贴:P – Kritner 2014-09-03 13:56:20

+0

是的非常感谢你,我已经编辑它现在再次PLZ再次看到。 – ProgrammingNinja 2014-09-03 13:59:25

+0

其中是空引用发生? – Kritner 2014-09-03 14:03:46

来源:

PayHistories = (from ph in employee.PayHistories 
         select new EmployeePayHistoryListDTO 
         { 
          Id = ph.BusinessEntityId, 
          RateChangeDate = ph.RateChangeDate, 
          Rate = ph.Rate, 
          PayFrequency = ph.PayFrequency, 
          JobTitle = ph.Employee.JobTitle, 
          Gendre = ph.Employee.Gender 
         }).ToList(); 

你能把它:

PayHistories = (from ph in employee.PayHistories 
         select new EmployeePayHistoryListDTO 
         { 
          Id = ph.BusinessEntityId, 
          RateChangeDate = ph.RateChangeDate, 
          Rate = ph.Rate, 
          PayFrequency = ph.PayFrequency 

         }).ToList(); 

,看看是否异常仍发生?

它看起来像基于该查询你会:

Employee -> PaymentHistory -> Employee. 

在你的声明:

_context.Employees.Include("PayHistories") 
           .Include("PayHistories")         
           .Single(e=>e.BusinessEntityId==id); 

它看起来并不像你将包括在顶部的额外员工的对象PayHistories(你有没有将它包含两次?)。我相信你也可以使用LINQ语句来获得更强类型包括像

.Include(i => i.PayHistories) 

希望这会帮助你!

+0

如果我按照你的建议。包括(i => i.PayHistories)我得到以下“不能转换类型字符串的lamda表达式,因为它不是委托类型” – ProgrammingNinja 2014-09-03 14:14:35

+1

显然,Include是一种扩展方法,您需要到“使用System.Data.Entity”来使用它。 PITA我知道,我总是忘记它的名字空间,并且你不能右键点击解析。 http://*.com/questions/4544756/using-include-in-entity-framework-4-with-lambda-expressions – Kritner 2014-09-03 14:27:02

+0

在这里,你是正确的100%非常感谢你,但问题仍然存在@Kritner。 – ProgrammingNinja 2014-09-03 14:37:11

确保employee.PayH istories不包含空项,或检查它的查询中:

PayHistories = (from ph in employee.PayHistories where null != ph 
etc. . . 

而且,你指的是对“PH”(员工),也可以为null一个可能延迟加载/未初始化的属性。

+0

如果员工为空,则仍会抛出异常。 – 2014-09-03 14:06:22

+0

这是正确的 - 我会将奇怪的SLQy linq代码转换为扩展方法调用,以便在错误检查中获得更多的灵活性。 – t0yk4t 2014-09-03 14:07:43

看起来您没有初始化您的员工对象。当空引用异常发生时,您应该可以通过将鼠标悬停在其上并检查它是否为空来检查员工对象的值。当您尝试访问空对象(在本例中为PayHistories)上的字段时,会出现null引用异常。

看是否有此代码避免了异常:

if(employee!=null){ 
    if(employee.PayHistories.Any()){ 

     PayHistories = (from ph in employee.PayHistories 
         select new EmployeePayHistoryListDTO 
         { 
          Id = ph.BusinessEntityId, 
          RateChangeDate = ph.RateChangeDate, 
          Rate = ph.Rate, 
          PayFrequency = ph.PayFrequency, 
          JobTitle = ph.Employee.JobTitle, 
          Gendre = ph.Employee.Gender 
         }).ToList(); 
    } 
} 

这是我如何解决我的问题。我有多个引用那些没有正确加载。

有两种方法

方法1.

的包括扩展方法过载它接受一个Expression<Func<T,U>>

_context.Employees.Include("PayHistories.furtherreferencename").Include("PayHistories.furtherReference"); 

方法2.

使用强类型包括方法。

_context.Employees.Include(i=>i.PayHistories.select(e=>e.FurtherReference)).Include(i=>i.PayHistories.select(e=>e.FurtherReference));