日期:2014-05-18  浏览次数:20471 次

如何获得一条DataTable重复数最多的记录
在MS SQL中用select top 3 tID from table group by tid order by count(tID) desc可以获得3条重复最多的数据
请问在DataTable中如何达到这个效果?如何获得一条DataTable重复数最多的记录

------解决方案--------------------
想了个笨方法
ArrayList alRow = new ArrayList();
ArrayList alCount = new ArrayList();
foreach (DataRow row in dt.Rows)
{
bool has = false;
for (int i = 0; i < alRow.Count; i++)
{
if (row["tID"] == ((DataRow)alRow[i])["tID"])
{
alCount[i] = (int)alCount[i] + 1;
has = true;
break;
}
}
if (has == false)
{
alRow.Add(row);
alCount.Add(1);
}
}
alCount 里面存的是alRow中对应的DataRow的重复数
------解决方案--------------------
可以在存储过程里写的 group by 时增加一列 counts 显示相同记录的条数