skip to Main Content

I have a ClassA with 10 fields and have the following list populated with 100 records.

List<ClassA> abc = new List<ClassA>();

I have another class ClassB with 12 fields where 10 fields are same as Class and 2 extra fields which are lets say harcoded ,viz field11 and field12. Property names of ClassA and ClassB are same.

I have tried looping ClassA list and build ClassB object inside the loop and keep adding to the list of ClassB with 2 extra columns.
But is there any better way to achieve this?

2

Answers


  1. Yes, you can use auto mapping libraries. Like AutoMapper or Mapster
    They can map your entities by properties naming convention. And also you can setup specific rules for each mapping.
    Please look for documentation in above links.

    Login or Signup to reply.
  2. you can use select :

    var def = abc.Select(a => new ClassB{
    field1 = a.field1,
    field2 = a.field2,
    .... 
    field11 = default,
    field12 = default,
    }).ToList()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search