我已经使用OleDb从用户上传中检索了两个Excel文件。我产生了以下结果:
| Location | Item Type | AmountA | AmountB | Type |
| A | A | 5 | 4 | |
但是我想产生以下结果:
| Location | Item Type | AmountA | AmountB | Type |
| A | A | 5 | | A |
| A | A | | 4 | B |
这些是我的代码:
public DataTable CombineofAdjustmentNTransaction(DataTable A, DataTable B)
{
DataTable TableE = new DataTable();
TableE.Columns.Add(new DataColumn("Location"));
TableE.Columns.Add(new DataColumn("Item Type"));
TableE.Columns.Add(new DataColumn("AmountA)"));
TableE.Columns.Add(new DataColumn("AmountB"));
TableE.Columns.Add(new DataColumn("TransactionType"));
foreach (DataRow dtE in A.Rows)
{
foreach (DataRow rowB in B.Rows)
{
if (rowB["Location"].ToString() == dtE["Location"].ToString() && rowB["Item Type"].ToString() == dtE["Item Type"].ToString()
)
{
var newRow = TableE.NewRow();
newRow["Location"] = dtE["Location"];
newRow["Item Type"] = dtE["Item Type"];
if(dtE["Type"].ToString() == "GRN")
{
newRow["AmountA"] = dtE["AmountA"];
newRow["Type"] = "GRN";
}
if (rowB["Type"].ToString() == "STK_ADJ")
{
newRow["AmountB"] = rowB["AmountB"];
newRow["Type"] = "STK_ADJ";
}
TableE.Rows.Add(newRow);
}
}
}
return TableE;
}
}
请帮助谢谢!
答案 0 :(得分:0)
public DataTable CombineofAdjustmentNTransaction(DataTable A, DataTable B)
{
DataTable TableE = new DataTable();
TableE.Columns.Add(new DataColumn("Location"));
TableE.Columns.Add(new DataColumn("Item Type"));
TableE.Columns.Add(new DataColumn("AmountA"));
TableE.Columns.Add(new DataColumn("AmountB"));
TableE.Columns.Add(new DataColumn("Type"));
foreach (DataRow dtE in A.Rows)
{
foreach (DataRow rowB in B.Rows)
{
if (rowB["Location"].ToString() == dtE["Location"].ToString() && rowB["Item Type"].ToString() == dtE["Item Type"].ToString()
)
{
var newRow = TableE.NewRow();
newRow["Location"] = dtE["Location"];
newRow["Item Type"] = dtE["Item Type"];
if (dtE["Type"].ToString() == "GRN")
{
newRow["AmountA"] = dtE["AmountA"];
newRow["Type"] = "A";
}
if (rowB["Type"].ToString() == "STK_ADJ" && newRow["AmountA"].ToString() != "" && newRow["AmountA"].ToString() != "")
{
var BNewRow = TableE.NewRow();
BNewRow["Location"] = rowB["Location"];
BNewRow["Item Type"] = rowB["Item Type"];
BNewRow["AmountB"] = rowB["AmountB"];
BNewRow["Type"] = "B";
TableE.Rows.Add(BNewRow);
}
else {
newRow["AmountB"] = rowB["AmountB"];
newRow["Type"] = "B";
}
TableE.Rows.Add(newRow);
}
}
}
return TableE;
}
答案 1 :(得分:0)
在您的代码中,您只需调用一次
DataTable.Rows.Add()
方法。这就是为什么只得到一行的原因。
在下面的解决方案中,您会注意到我已经两次调用了此方法
result.Rows.Add(newRowA);
result.Rows.Add(newRowB);
这是为您提供的完整重构方法。
// made these into constants, note how it reads easier when prefixing column numbers
// added benefit of renaming the columns in a single place
const string Column1Location = "Location";
const string Column2ItemType = "Item Type";
const string Column3AmountA = "Amount A";
const string Column4AmountB = "Amount B";
const string Column5TransactionType = "Transaction Type";
public DataTable CombineofAdjustmentNTransaction(DataTable tableA, DataTable tableB)
{
DataTable result = new DataTable();
result.Columns.Add(new DataColumn(Column1Location));
result.Columns.Add(new DataColumn(Column2ItemType));
result.Columns.Add(new DataColumn(Column3AmountA));
result.Columns.Add(new DataColumn(Column4AmountB));
result.Columns.Add(new DataColumn(Column5TransactionType));
foreach (DataRow rowA in tableA.Rows)
foreach (DataRow rowB in tableB.Rows)
{
// check for required conditions, skip if failed (continue to the next iteration)
if (rowA["Location"] != rowB["Location"]) continue;
if (rowA["ItemType"] != rowB["Item Type"]) continue;
// your logic for A
if (rowA["Type"].ToString() == "GRN")
{
// create row
DataRow newRowA = result.NewRow();
newRowA[Column1Location] = rowA["Location"];
newRowA[Column2ItemType] = rowA["Item Type"];
newRowA[Column3AmountA] = rowA["AmountA"];
newRowA[Column4AmountB] = null;
newRowA[Column5TransactionType] = "GRN";
// add the row
result.Rows.Add(newRowA);
}
// your logic for B
if (rowB["Type"].ToString() == "STK_ADJ")
{
// create row
DataRow newRowB = result.NewRow();
newRowB[Column1Location] = rowA["Location"];
newRowB[Column2ItemType] = rowA["Item Type"];
newRowB[Column3AmountA] = null;
newRowB[Column4AmountB] = rowB["AmountB"];
newRowB[Column5TransactionType] = "STK_ADJ";
// add the row
result.Rows.Add(newRowB);
}
}
return result;
}