[SOLVED] Building a List from another list and other sources in C#

Issue

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?

Solution

you can use select :

var def = abc.Select(a => new ClassB{
field1 = a.field1,
field2 = a.field2,
.... 
field11 = default,
field12 = default,
}).ToList()

Answered By – mostafa khoramnia

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *