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?
CodePudding user response:
you can use select :
var def = abc.Select(a => new ClassB{
field1 = a.field1,
field2 = a.field2,
....
field11 = default,
field12 = default,
}).ToList()
CodePudding user response:
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.