将列表对象复制到另一个列表对象的对象中 [英] Copy list object into object of another list object

查看:151
本文介绍了将列表对象复制到另一个列表对象的对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有两个列表对象,例如List< UPDTO>和列表< PDTO>

PDTO类
{
公共字符串Prop1 {get;设置;}
}

UPDTO类
{
公开PDTO oPDTO {获取;设置;}
}


我在List< PDTO>中有数据.并且我希望将其复制到List< UPDTO>中具有的对象(PDTO).您能告诉我解决方案吗?

我们可以像下面这样做:

foreach(oListPDTO中的PDTO oPDTO1)
{
oListUPDTO.Add(new UPDTO(){oPDTO = oPDTO1});
}
有什么更好的方法吗?


谢谢,
Mukesh KV

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

推荐答案

我假设您的UPDto和PDto是具有不同属性的不同类型,在这种情况下,您可能需要考虑深度对象处理.以下链接可以帮助您进行深度对象复制,

C#中对象的深层副本 [
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 :)


穆罕默德,
感谢您的解决方案.但是我相信这不是我所需要的.一样可以
通过问题中提到的foreach循环实现.

PDTO类
{
公共字符串Prop1 {get;设置;}
}
UPDTO类
{
公开PDTO oPDTO {获取;设置;}
}

类MyClass
{
Method1()
{
列表< PDTO> oListPDTO = new List< PDTO>();
列表< UPDTO> oListUPDTO =新列表< UPDTO>();

foreach(oListPDTO中的PDTO obj)
{
UPDTO oUPDTO =新的UPDTO();
oUPDTO.oPDTO = obj;
oListUPDTO.Add(oUPDTO);
}
}
}

有没有一种更好的方法可以代替foreach循环??????


谢谢,
Mukesh KV
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


这篇关于将列表对象复制到另一个列表对象的对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆