I have a List of MyType1 objects. I want to copy all these objects in a new List of MyType2 objects, having a constructor for MyType2 that takes a MyType1 object as argument.
For now, I do it this way:
List<MyType1> type1objects = new List<MyType1>();
// Fill type1objects
// ...
List<MyType2> type2objects = new List<MyType2>();
foreach (MyType1 type1obj in type1objects) {
MyType2 type2obj = new MyType2(type1obj);
type2objects.Add(type2obj);
}
Is there a way to do this one-line (I’m thinking maybe it is possible with Linq)?
1 Answer