简体   繁体   English

使用LINQ和C#将行从一个表复制到另一个表

[英]Copying a row from one table to another using LINQ and C#

I have two tables that are pretty much exact clones of one another (identical columns, just different columns set as primary keys). 我有两个表,它们几乎是彼此的精确克隆(相同的列,只是将不同的列设置为主键)。 Basically the second table is just for keeping a history of the first table. 基本上,第二个表仅用于保留第一个表的历史记录。 What I need to do is, when a user updates a record in table 1 I need to insert the original copy of that record into table 2. 我需要做的是,当用户更新表1中的记录时,我需要将该记录的原始副本插入表2中。

I am using a LinqDataSource object and utilizing the LinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e) event so I have access to e.OriginalObject and that will be perfect for inserting the original row in table 2. My problem is that I don't want to have to set every property manually because there are about 50 of them, so I want to use Reflection but am not sure how to properly go about it. 我正在使用LinqDataSource对象,并利用LinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)事件,所以我可以访问e.OriginalObject ,这对于在表2中插入原始行是完美的。我的问题是我不想必须手动设置每个属性,因为其中大约有50个,因此我想使用Reflection,但不确定如何正确处理它。

Consider the following code: 考虑以下代码:

INSTRUMENT_DATA_SHEET _original = (INSTRUMENT_DATA_SHEET)e.OriginalObject;
INSTRUMENT_DATA_SHEET_HISTORY _history = new INSTRUMENT_DATA_SHEET_HISTORY();

How can I go about copying all of the _original 's property values to _history 's? 如何将所有_original的属性值复制到_history I have tried using the solution from this question , however it isn't working for me. 我已尝试使用此问题的解决方案,但是它不适用于我。 It throws the error: 它引发错误:

Property DATE has an incompatible type in E_and_I.INSTRUMENT_DATA_SHEET_HISTORY

My guess is that it's because the DATE column is part of the primary key in table 2, but not table 1. As I said, the only difference between the two tables are the primary keys. 我的猜测是,这是因为DATE列是表2中主键的一部分,而不是表1中的主键。正如我所说,两个表之间的唯一区别是主键。 Here they are for your reference: 这里供您参考:

主键

The problem I see is that your History type Date field is DateTime and your Original one is DateTime? 我看到的问题是,您的“历史记录”类型的“日期”字段是DateTime而您的“原始”类型是DateTime? (same problem with REV in History, it can't be null). (与“历史记录”中的REV相同的问题,不能为null)。 You'll have to decide what happens if there is a null DateTime of Date in your original version. 如果原始版本中的DateTime为空,则必须决定会发生什么。 Then you should be able to modify Skeets code (oh dear!) to handle specifically these fields differently then the rest of the fields. 然后,您应该能够修改Skeets代码(哦,亲爱的!),以与其他字段不同的方式专门处理这些字段。

Ok I've managed to figure it out :) Here's what I did: 好了,我设法弄清楚了:)这是我做的:

INSTRUMENT_DATA_SHEET _original = (INSTRUMENT_DATA_SHEET)e.OriginalObject;
INSTRUMENT_DATA_SHEET_HISTORY _history = new INSTRUMENT_DATA_SHEET_HISTORY();

foreach (PropertyInfo pi in _original.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
    _history.GetType().GetProperty(pi.Name).SetValue(_history, pi.GetValue(_original, null), null);
}

Not very elegant but it gets the job done! 不是很优雅,但是可以完成工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM