Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在实体框架中将一个类类型的集合转换为另一个类类型集合_C#_Linq_Entity Framework_Dto - Fatal编程技术网

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

C# 如何在实体框架中将一个类类型的集合转换为另一个类类型集合,c#,linq,entity-framework,dto,C#,Linq,Entity Framework,Dto,我正在开发WebAPI,在这里我必须创建数据传输对象,以便在应用程序的UI上显示数据 我正在使用代码优先的方法,这里是我的域类 public class Employee { [Key] public int BusinessEntityId { get; set; } [Required] [MaxLength(50)] public string JobTitle { get; set; }

我正在开发WebAPI,在这里我必须创建数据传输对象,以便在应用程序的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; }

    }
现在,由于PayHistories是我的域类中的集合,我正在做的是创建一个新类 其中包含my DTO类类型的集合
EmployeePayHistoryListDTO

 public class EmployeeRelatedCollections
    {
        public ICollection<EmployeePayHistoryListDTO> PayHistories { get; set; }
    }
但是,当我将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 

 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.

确保employee.PayHistories不包含空条目,或在查询中进行检查:

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

此外,您正在引用“ph”(Employee)上可能延迟加载/未初始化的属性,该属性也可能为null。

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

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

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();
    }
}
发件人:

你能做到:

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);
看起来你不会在你的工资历史记录中包含额外的雇员对象(你有没有打算包含两次?)。我相信您也可以使用linq语句来获得更强类型的包含,如

.Include(i => i.PayHistories)

希望这能帮助你

以下是我解决问题的方法。我有多个引用没有正确加载

有两种方法

方法1.

Include扩展方法重载,它采用
表达式

方法2.

使用强类型Include方法


\u context.Employees.Include(i=>i.PayHistories.select(e=>e.FutureReference)).Include(i=>i.PayHistories.select(e=>e.FutureReference))

我想你错过了你的错误粘贴:PYes非常感谢我现在再次编辑了它。请再次查看。空引用发生在哪里?如果employee为空,这仍然会引发异常。这是正确的-我会将奇怪的SLQy linq代码转换为扩展方法调用,以便在错误检查中获得更大的灵活性。如果我这样做的话您建议.Include(i=>i.PayHistories)我得到以下“不能转换字符串类型的lamda表达式,因为它不是委托类型”显然Include是一个扩展方法,您需要“使用System.Data.Entity”来使用它。PITA我知道,我总是忘记它的名称空间,而且你不能右键单击resolve。这里你是100%正确的,非常感谢。但问题仍然存在@Kritner。
Employee -> PaymentHistory -> Employee.
_context.Employees.Include("PayHistories")
                                 .Include("PayHistories")                                 
                                 .Single(e=>e.BusinessEntityId==id);
.Include(i => i.PayHistories)
_context.Employees.Include("PayHistories.furtherreferencename").Include("PayHistories.furtherReference");