Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将一个对象复制到另一个对象_C#_.net_Wpf - Fatal编程技术网

C# 将一个对象复制到另一个对象

C# 将一个对象复制到另一个对象,c#,.net,wpf,C#,.net,Wpf,我有两个表,user和userprofile。userprofile表有许多类似于user表的字段。我需要做的是,单击一个按钮,我需要将user表的所有字段复制到userprofile表 How can I do that? 您能否在UserProfile上创建一个构造函数,将用户实例作为参数并在那里执行?不确定这是最好的方法,或者这是否违反了良好的设计。这里有一个实用程序,它擅长推断两个看似不相关的对象之间的链接,你可能想检查一下,因为它非常酷。你能在UserProfile上创建一个构造函数

我有两个表,user和userprofile。userprofile表有许多类似于user表的字段。我需要做的是,单击一个按钮,我需要将user表的所有字段复制到userprofile表

How can I do that?

您能否在UserProfile上创建一个构造函数,将用户实例作为参数并在那里执行?不确定这是最好的方法,或者这是否违反了良好的设计。这里有一个实用程序,它擅长推断两个看似不相关的对象之间的链接,你可能想检查一下,因为它非常酷。

你能在UserProfile上创建一个构造函数,将用户实例作为参数并在那里执行吗?不确定这是最好的方法,或者这是否违反了良好的设计。有一个叫做There的实用程序,它擅长于推断两个看似不相关的对象之间的链接,您可能想检查一下,因为它非常酷。

这可能过于简单化了,但是什么阻止了您呢

UserProfile profile = new UserProfile();
profile.Name = UserModel[0].Name;
profile.OtherProperty = UserModel[0].OtherProperty;
...
DataServices.Save(profile);

如果这是一个封装问题,并且您无法修改这些属性,那么Rob关于创建一个接受用户对象的构造函数的回答听起来很合适。

也许这过于简单,但是什么阻止了您

UserProfile profile = new UserProfile();
profile.Name = UserModel[0].Name;
profile.OtherProperty = UserModel[0].OtherProperty;
...
DataServices.Save(profile);

如果这是一个封装问题,并且您无法修改这些属性,那么Rob关于创建接受用户对象的构造函数的回答听起来很合适。

也许我没有完全理解您的请求。但是如果你有两张桌子,比如

DataTable user;
DataTable userProfiles;
您希望userProfiles包含与表1相同的字段或列

userProfiles= user.Clone();  // This will copy the schema of user table
userProfiles= user.Copy();   // This will copy the schema AND data of user table 
现在,如果要在某些行上进行复制,则可以执行以下操作

DataRow dr;
userProfiles= user.Clone();   // Do this first to create the schema.
foreach(DataRow row in user.Rows)
{
   if(...) // code to determine if you want to add this row
   {
      userProfiles.Rows.Add(row);  // This will add the same row from user table to userProfiles; Or you could create a new row using the 'dr' above and copy the data to provide a new DataRow
   }
}

也许我没有完全理解你的要求。但是如果你有两张桌子,比如

DataTable user;
DataTable userProfiles;
您希望userProfiles包含与表1相同的字段或列

userProfiles= user.Clone();  // This will copy the schema of user table
userProfiles= user.Copy();   // This will copy the schema AND data of user table 
现在,如果要在某些行上进行复制,则可以执行以下操作

DataRow dr;
userProfiles= user.Clone();   // Do this first to create the schema.
foreach(DataRow row in user.Rows)
{
   if(...) // code to determine if you want to add this row
   {
      userProfiles.Rows.Add(row);  // This will add the same row from user table to userProfiles; Or you could create a new row using the 'dr' above and copy the data to provide a new DataRow
   }
}

为什么这被标记为WPF问题?为什么这被标记为WPF问题?Automapper非常适合这种情况,尤其是当您不想创建链接这两个对象的构造函数时。Automapper非常适合这种情况,尤其是当您不想创建链接这两个对象的构造函数时。谢谢您的回答。我唯一关心的是,表中有很多字段,因此如果有一些简单的方法可以做到这一点,那将更加简单和干净。在这种情况下,每次我更改表中的字段时,我都必须去编辑代码。有什么方法可以让我用反思来达到这个目的吗?@developer,你看过Rob的答案了吗?我没有看过AutoMapper,但Chad和Rob都认为它对你有用。谢谢你的答案。我唯一关心的是,表中有很多字段,因此如果有一些简单的方法可以做到这一点,那将更加简单和干净。在这种情况下,每次我更改表中的字段时,我都必须去编辑代码。有什么方法可以让我用反思来达到这个目的吗?@developer,你看过Rob的答案了吗?我没有看过AutoMapper,但Chad和Rob似乎都认为它对你有用。