使用DataView,然后设置ToTable,设置几个字段和一个布尔值,表示这些字段作为一个整体,在这个表内不允许重复,示例代码:

namespace A
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id",typeof(int));
            Random r = new Random(DateTime.Now.Millisecond);
            //随机生成1000行记录
            for (int i = 0; i < 1001; i++)
            {
                dt.Rows.Add(r.Next(1, 11));
            }
 
            DataView dv = new DataView(dt);
            dt = dv.ToTable(true, "Id");    //对Id进行过滤,true表示使用distinct方法
 
            foreach (DataRow item in dt.Rows)
            {
                Console.WriteLine(item["Id"].ToString());
            }
        }
    }
}

 上面datatable只有一列,如果是多列的话,去重如下

DataView dv = new DataView(dt);
dt = dv.ToTable(true, new string[] { "Id","Value" });

 

相关文章:

  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2021-06-22
  • 2021-11-29
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
  • 2021-07-31
  • 2022-12-23
相关资源
相似解决方案