Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have two list objects, say List<UPDTO> and List<PDTO>

class PDTO
{
public string Prop1{get; set;}
}

class UPDTO
{
public PDTO oPDTO {get; set;}
}


I have data in List<PDTO>. And I want this to get copied to the object (PDTO) that I have in List<UPDTO>. Could you please let me know the solution?

We can do the same like the below:

foreach (PDTO oPDTO1 in oListPDTO)
{
oListUPDTO.Add(new UPDTO() { oPDTO = oPDTO1 });
}
Is there any better way of doing this?


Thanks,
Mukesh KV
Posted

I assume your UPDto and PDto is different types with different properties, in this situation you might need to consider Deep object coping. Following link might help you to do the Deep object copying,

Deep copy of objects in C#[^]

Hope it helps :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-11 4:48am    
Agree, my 5. OP replied.
(OP simply does not understand it's still a kind of Deep cloning between unrelated types.)
--SA
Hi Mohammad,
Thanks for your solution. But I believe that is not what I need. The same can
be acheived through the foreach loop mentioned in the question.

class PDTO
{
public string Prop1{get; set;}
}
class UPDTO
{
public PDTO oPDTO {get; set;}
}

class MyClass
{
Method1()
{
List<PDTO> oListPDTO = new List<PDTO>();
List<UPDTO> oListUPDTO = new List<UPDTO>();

foreach (PDTO obj in oListPDTO)
{
UPDTO oUPDTO = new UPDTO();
oUPDTO.oPDTO = obj;
oListUPDTO.Add(oUPDTO);
}
}
}

Is there a better way of doing this instead of foreach loop??????


Thanks,
Mukesh KV
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-11 4:47am    
You simply don't realize that your oPDTO is the deep cloning.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900