我从2个不同的数据库(Oralce和MS SQL)里查询得到1个数据集的2个表
我把这个数据集的2个表合并在一起得到一个新表,现在这个新表的结构是这样的:
DataTable MyTable = ds.Tables[2]; //ds为数据集
Item Description Uom Quantity
a1 a1's description EA 10.22
a2 a2's description EA 11.00
a3 a3's description KG 25.00
a4 a4's description KG 0.23
a5 a5's description KG 1.00
a1 a1's description EA 0.25
a1 a1's description EA 56.55
a3 a3's description KG 23.65
a4 a4's description KG 25.36
我现在想把这个MyTable中字段Item重复行去掉,但是又要把Quantity的和加起来
即将MyTable变为(或者填充到ds的另一个表使变为):
Item Description Uom Quantity
a1 a1's description EA 67.02 \\(10.22 + 0.25 + 56.55)
a2 a2's description EA 11.00
a3 a3's description KG 48.65 \\(25.00 + 23.65)
a4 a4's description KG 25.59 \\(0.23 + 25.36)
a5 a5's description KG 1.00 请哪位高手帮忙解决下,不胜感激!
我把这个数据集的2个表合并在一起得到一个新表,现在这个新表的结构是这样的:
DataTable MyTable = ds.Tables[2]; //ds为数据集
Item Description Uom Quantity
a1 a1's description EA 10.22
a2 a2's description EA 11.00
a3 a3's description KG 25.00
a4 a4's description KG 0.23
a5 a5's description KG 1.00
a1 a1's description EA 0.25
a1 a1's description EA 56.55
a3 a3's description KG 23.65
a4 a4's description KG 25.36
我现在想把这个MyTable中字段Item重复行去掉,但是又要把Quantity的和加起来
即将MyTable变为(或者填充到ds的另一个表使变为):
Item Description Uom Quantity
a1 a1's description EA 67.02 \\(10.22 + 0.25 + 56.55)
a2 a2's description EA 11.00
a3 a3's description KG 48.65 \\(25.00 + 23.65)
a4 a4's description KG 25.59 \\(0.23 + 25.36)
a5 a5's description KG 1.00 请哪位高手帮忙解决下,不胜感激!
可以做到,但是出现了中文字乱码的问题
因此我想看可不可以不通过SQL直接做到这一点
比如:
原来的表是T1,我在T1中去掉重复行并求和后放入T2
for (int i = 0, j = ds.Tables["T1"].Rows.Count; i<j; i++)
{
string tempitem = ds.Tables[0].Rows[i][0].ToString();
DataRow[] rows;
rows = ds.Tables["T1"].Select("Item = '" + tempitem + "' ");
if (rows.Length > 1)
{
\\我选择出了重复的行,怎么求和呢
}
\\这里面不会写了,惭愧
}
{
object tempitem=ds.Tables["T1"].Rows[i][0];
for(int j=i;j<ds.Tables["T1"].Rows.Count;j++)
{
if(tempitem == ds.Tables["T1"].Rows[j][0])
{
ds.Tables["T1"].Rows[i][3] = (int)ds.Tables["T1"].Rows[i][3] + (int)ds.Tables["T1"].Rows[j][3];
ds.Tables["T1"].Rows.RemoveAt(j);
}
}
}
=========================
不知道会出错没。
没测试。
DataView dv = new DataView(dtClone);
dv.Sort = "Item";
foreach(DataRow dr in MyTable.Rows)
{
int rowIndex = dv.FindRow(dr["Item"]);
if (rowIndex == -1)
{
dtClone.ImportRow(MyTable.Rows[i]);
dtClone.Rows[dtClone.Rows - 1]["Quantity"] = (decimal)MyTable.Compute("Sum(Quantity)", string.Format("Item = '{0}'", dr["Item"].ToString());
}
}
dtClone.AcceptChanges();ds.Tables.Remove(MyTable);
ds.Tables.Add(dtClone);
==
随手写了一下 没测试
DataView dv = new DataView(dtClone);
dv.Sort = "Item";
foreach(DataRow dr in MyTable.Rows)
{
int rowIndex = dv.FindRow(dr["Item"]);
if (rowIndex == -1)
{
dtClone.ImportRow(dr);
dtClone.Rows[dtClone.Rows - 1]["Quantity"] = (decimal)MyTable.Compute("Sum(Quantity)", string.Format("Item = '{0}'", dr["Item"].ToString());
}
}
dtClone.AcceptChanges();ds.Tables.Remove(MyTable);
ds.Tables.Add(dtClone);
for (int i = 0; i < ds.Tables["T1"].Rows.Count; i++)
{
tring tempitem = ds.Tables["T1"].Rows[i][0].ToString();
for (int j = i + 1; j < ds.Tables["T1"].Rows.Count; j++)
{
if (tempitem == ds.Tables["T1"].Rows[j][0].ToString())
{
ds.Tables["T1"].Rows[i][2] = float.Parse(ds.Tables["T1"].Rows[i][2].ToString()) + float.Parse(ds.Tables["T1"].Rows[j][2].ToString());
ds.Tables["T1"].Rows.RemoveAt(j);
j = j - 1;
}
}
}
我试下zxkid的代码
准备结帖,请各位再看下我写的有什么要更改的地方(我测试已经通过了),谢谢各位!
你好,我有点愚钝,可以给点注释吗?谢谢!
还还看不懂删除重复行怎么实现的,不好意思!
DataView dv = new DataView(dtClone); //用于查找
dv.Sort = "Item";
foreach(DataRow dr in MyTable.Rows) //单循环, 效率高一点
{
int rowIndex = dv.FindRow(dr["Item"]);
if (rowIndex == -1)
{
dtClone.ImportRow(dr); //添加行
dtClone.Rows[dtClone.Rows - 1]["Quantity"] = (decimal)MyTable.Compute("Sum(Quantity)", string.Format("Item = '{0}'", dr["Item"].ToString());//合计
}
}
dtClone.AcceptChanges();ds.Tables.Remove(MyTable); //删除旧表
ds.Tables.Add(dtClone); //添加新表