2013-12-19 23 views
2

我有兩個Table1和Table2,其列的值如下: 這兩個表: 我想更新Table1中的「PaidAmount」。根據兩個表中的「InvoiceNo」列加入。加入並更新數據表

表1:

InvoiceNo Vendor InvoiceValue InvoiceDate  PaidAmount 
--------- ------ ------------- -----------  ---------- 
    1  AA  15000  01 Dec 2013   0  
    2  BB  20000  10 Dec 2013   0  
    3  CC  10000  18 Dec 2013   0  

表2:

VoucherNo Date  InvoiceNo Amount 
------- ----------- ---------- ------ 
    001  01 Nov 2013 1   5000 
    002  10 Nov 2013 2   6000 
    003  20 Nov 2013 3   7000 
    001  02 Nov 2013 1   2000 

我想要的數據表應該是這樣的:

DesiredTable:

InvoiceNo Vendor InvoiceValue InvoiceDate  PaidAmount 
--------- ------ ------------- -----------  ---------- 
    1  AA  15000  01 Dec 2013  7000  
    2  BB  20000  10 Dec 2013  6000 
    3  CC  10000  18 Dec 2013  7000 

如何實現這個結果? 我已經嘗試了下面的一個。

Table1.AsEnumerable().Join(Table2.AsEnumerable(), 
         dt1_Row => dt1_Row.ItemArray[0], 
         dt2_Row => dt2_Row.ItemArray[2], 
         dt1_Row, dt2_Row) => new { dt1_Row, dt2_Row } 
        ).ToList() 
        .ForEach(o => o.dt1_Row.SetField(4, o.dt2_Row.ItemArray[3])); 

但它給出結果作爲

InvoiceNo Vendor InvoiceValue InvoiceDate  PaidAmount 
--------- ------ ------------- -----------  ---------- 
    1  AA  15000  01 Dec 2013  2000  
    2  BB  20000  10 Dec 2013  6000 
    3  CC  10000  18 Dec 2013  7000 

如何獲得我想要的表?

回答

1

你的加入給你一個多行1,row2夫婦的列表。
因此,您正在循環每對夫婦,第一次爲invoiceNo1,row1.PaidAmount = 5000,然後您的循環繼續,第二次,row1.PaidAmount = 2000,所以你的結果。

你要總結ROW2的金額值,於是加盟後,你必須按InvoiceValue的DATAS,然後執行總和:

foreach(var grp in Table1.AsEnumerable() 
     .Join(Table2.AsEnumerable(), 
      dt1_Row => dt1_Row.ItemArray[0], 
      dt2_Row => dt2_Row.ItemArray[2], 
      dt1_Row, dt2_Row) => new { dt1_Row, dt2_Row } 
     ) 
     .GroupBy(o => o.dt1_Row.ItemArray[0])) 
{ 
    var row1 = grp.First().dt1_Row; 
    var sum = grp.Sum(t => Convert.ToDecimal(t.dt2_Row.ItemArray[3])); 
    row1.SetField(4, sum) 
} 

更多可讀性,儘量避免使用列表的ForEach它並不能真正改善你的代碼。

+0

它給出了錯誤:不能隱式地將類型'object'轉換爲'decimal'。 – thevan

+0

我已經更新了從對象到int的轉換代碼。字段金額的類型是什麼? –

+0

decimal是數量和paidamount的數據類型。 – thevan

1

它看起來像你迭代你的結果和覆蓋,而不是添加到其他行的預先存在的結果。你可能會想這樣做:

Table1.AsEnumerable().Join(Table2.AsEnumerable(), 
         dt1_Row => dt1_Row.ItemArray[0], 
         dt2_Row => dt2_Row.ItemArray[2], 
         dt1_Row, dt2_Row) => new { dt1_Row, dt2_Row } 
        ).ToList() 
        .ForEach(o => o.dt1_Row.SetField(4, o.dt1_Row.ItemArray[4] + o.dt2_Row.ItemArray[3])); 
+0

它給出了以下錯誤:「Error 運算符'+'不能應用於類型爲'object'和'object'的操作數」 – thevan

+0

您需要獲取ItemArray中對象的值。我不知道你的ItemArray中的項目的底層類型是什麼。您可能需要將其轉換爲適當的類型。 – Carth

1

下面是一種方法。您可以使用GroupBy創建發票組,並使用ToDictionary創建高效的InvoiceNoPaidAmount查找。然後你只需要一個循環更新行:

var invoiceGroups = from r1 in Table1.AsEnumerable() 
        join r2 in Table2.AsEnumerable() 
        on r1.Field<int>("InvoiceNo") equals r2.Field<int>("InvoiceNo") 
        group r2 by r2.Field<int>("InvoiceNo") into InvoiceGroup 
        select InvoiceGroup; 
Dictionary<int, int> invoiceSumLookup = invoiceGroups 
    .ToDictionary(ig => ig.Key, ig => ig.Sum(r => r.Field<int>("Amount"))); 
foreach(DataRow row in Table1.Rows) 
    row.SetField("PaidAmount", invoiceSumLookup[row.Field<int>("InvoiceNo")]); 

請注意,這並不Table1刪除重複InvoiceNo行如果可能的話,而不是期望的。它只是將該列中的第二個表分組並總計所有Amount s。

它修改原始表並不需要創建一個新表。