没有合适的资源?快使用搜索试试~ 我知道了~
C# DataTable去重,根据列名去重保留其他列
1星 需积分: 50 30 下载量 7 浏览量
2018-07-09
20:15:34
上传
评论
收藏 2KB TXT 举报
温馨提示
试读
2页
详细描述如何使用C# 去除DataTable中的重复列,根据列名去重保留其他列
资源推荐
资源详情
资源评论
#region 删除DataTable重复列,类似distinct
/// <summary>
/// 删除DataTable重复列,类似distinct
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="Field">字段名</param>
/// <returns></returns>
public static DataTable DeleteSameRow(DataTable dt, string Field)
{
ArrayList indexList = new ArrayList();
// 找出待删除的行索引
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
if (!IsContain(indexList, i))
{
for (int j = i + 1; j < dt.Rows.Count; j++)
{
if (dt.Rows[i][Field].ToString() == dt.Rows[j][Field].ToString())
{
indexList.Add(j);
}
}
}
}
indexList.Sort();
// 排序
for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
{
int index = Convert.ToInt32(indexList[i]);
dt.Rows.RemoveAt(index);
/// <summary>
/// 删除DataTable重复列,类似distinct
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="Field">字段名</param>
/// <returns></returns>
public static DataTable DeleteSameRow(DataTable dt, string Field)
{
ArrayList indexList = new ArrayList();
// 找出待删除的行索引
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
if (!IsContain(indexList, i))
{
for (int j = i + 1; j < dt.Rows.Count; j++)
{
if (dt.Rows[i][Field].ToString() == dt.Rows[j][Field].ToString())
{
indexList.Add(j);
}
}
}
}
indexList.Sort();
// 排序
for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
{
int index = Convert.ToInt32(indexList[i]);
dt.Rows.RemoveAt(index);
资源评论
- csdner2018-07-21一点都不详细,仅一段代码而已。差评
Denny辉
- 粉丝: 201
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功