How to convert Collection of One Class type to Another Class Type collection in Entity Framework

ProgrammingNinja

I am working on Web Api where I would have to create Data Transfer Objects for displaying data on the UI of application.

I am working with Code First approach here is my Domain class

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

    }

Here is my Data Transfer Object(DTO) Class

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

    }

Now As PayHistories is collection in my Domain class I what i am doing is i am creating a new class of which has collection of my DTO class type EmployeePayHistoryListDTO

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

So from my repository I am getting data correctly via following EF statement

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

But where i am converting collection of my Employee class(Domain Class) to collection of my DTO type there i am getting error here is the code

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.
Kritner

From:

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();

Can you make it:

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

                        }).ToList();

and see if the exception still occurs?

It looks like based on that query you would have:

Employee -> PaymentHistory -> Employee.

In your statement:

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

It doesn't look like you would include the additional employee object on top of your PayHistories (and did you intentially include it twice?). I believe you could also use a linq statement to get more strongly typed includes like

.Include(i => i.PayHistories)

Hopefully this will help you!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert Collection of One Class type to Another Class Type collection in Entity Framework

From Dev

Entity Framework:Why the collection type of entity class need to be instanced in the default constructor?

From Dev

Casting One Collection Type to another Collection Type and iterate over it in Java

From Dev

How do I also specify a generic collection type for a generic class?

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

Type-safe collection of Type class with defined base type

From Dev

Collection class of specific type containing basic features

From Dev

IEnumerable Class Collection type conversion from List

From Dev

convert collection to array by type

From Dev

Can I use a class with a generic type to a collection of a different class?

From Dev

Why is entity framework not loading my collection into the base class?

From Dev

Data Conversion from one class type to another class type

From Dev

How can I map a derived class having a collection of entities with Entity Framework

From Dev

How to declare a class based on type of another class

From Dev

Silex Framework Collection form type

From Dev

Implement type casting from one class to another

From Dev

Passing generic type from one class to another?

From Dev

filter lisbox populated by an observable collection by the class type in c# wpf

From Dev

'Cannot initialize type with a collection initializer' while initializing object of my class

From Dev

filter lisbox populated by an observable collection by the class type in c# wpf

From Dev

How to create a collection of several fields without any custom Form Type class?

From Dev

How to convert one data type to another

From Dev

How to convert one type of object array into another?

From Dev

How to implicitly convert one class to another in Scala

From Dev

Entity Framework buddy class results in type conversion error

From Dev

How to convert the type for a number of fields in all documents in a collection?

From Dev

How to convert a class object to generic object type?

From Dev

How to convert nested case class into UDTValue type

Related Related

  1. 1

    How to convert Collection of One Class type to Another Class Type collection in Entity Framework

  2. 2

    Entity Framework:Why the collection type of entity class need to be instanced in the default constructor?

  3. 3

    Casting One Collection Type to another Collection Type and iterate over it in Java

  4. 4

    How do I also specify a generic collection type for a generic class?

  5. 5

    How to create a class that one of its object is in the type of another class in VB?

  6. 6

    How to create a class that one of its object is in the type of another class in VB?

  7. 7

    Type-safe collection of Type class with defined base type

  8. 8

    Collection class of specific type containing basic features

  9. 9

    IEnumerable Class Collection type conversion from List

  10. 10

    convert collection to array by type

  11. 11

    Can I use a class with a generic type to a collection of a different class?

  12. 12

    Why is entity framework not loading my collection into the base class?

  13. 13

    Data Conversion from one class type to another class type

  14. 14

    How can I map a derived class having a collection of entities with Entity Framework

  15. 15

    How to declare a class based on type of another class

  16. 16

    Silex Framework Collection form type

  17. 17

    Implement type casting from one class to another

  18. 18

    Passing generic type from one class to another?

  19. 19

    filter lisbox populated by an observable collection by the class type in c# wpf

  20. 20

    'Cannot initialize type with a collection initializer' while initializing object of my class

  21. 21

    filter lisbox populated by an observable collection by the class type in c# wpf

  22. 22

    How to create a collection of several fields without any custom Form Type class?

  23. 23

    How to convert one data type to another

  24. 24

    How to convert one type of object array into another?

  25. 25

    How to implicitly convert one class to another in Scala

  26. 26

    Entity Framework buddy class results in type conversion error

  27. 27

    How to convert the type for a number of fields in all documents in a collection?

  28. 28

    How to convert a class object to generic object type?

  29. 29

    How to convert nested case class into UDTValue type

HotTag

Archive