0
有時在SQL Server中,我們有一個包含大量列和另一個表('Production')的臨時導入表('Import')很多列和很多列都有相同的名稱(但不是全部)和,我們只需將同名的列的值從一個寫入另一個(不處理它們)。相同的數據庫,兩個表中同名的列具有相同的類型。如何將一個SQL Server表中的值複製到另一個使用LINQ的另一個同名列中
通常我們做這樣的:
// Lots and lots of columns copied without processing
Production.ColumnA = Import.ColumnA;
Production.ColumnB = Import.ColumnB;
Production.ColumnC = Import.ColumnC;
// [...]
// Some with processing
Production.ColumnQ1 = DoSomething(Import.ColumnQ2);
如果有很多列這很煩人,不知何故冗餘。什麼是自動,在這個僞代碼複製那些同樣命名的列最喜歡的方式:
foreach(var importRow in ImportRows)
foreach(Column col in Production.Columns)
if(Import.Columns.Contains(col.Name)) newProductionRow[col] = importRow[col];
(非常相似,人們可以使用帶有指數數據表做的,但我們不希望出現這種情況這裏)
+1爲完美的答案和偉大的啓動鏈接。屬性克隆是處理這類問題的最佳方式 – 2012-01-17 21:29:44
對我來說也很好。謝謝! – Olaf 2012-01-19 08:19:23