首页 > 代码库 > DataTable 过滤重复行

DataTable 过滤重复行

技术分享
   /// <summary>
        /// 去除重复数据
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public DataTable FilterRepeatTable(DataTable table)
        {

            DataTable _table = table.Clone();
            foreach (DataRow row in table.Rows)
            {
                //field1要去除重复的字段,可以是多个字段
                //int rowCount = (from DataRow q in _table.Rows where q["field1"].ToString() == row["field1"].ToString() && q["field2"].ToString() == row["field2"].ToString() select q).ToList().Count();
                int rowCount = (from DataRow q in _table.Rows where q["SBMCBH"].ToString() == row["SBMCBH"].ToString() select q).ToList().Count();
                if (rowCount == 0)
                {
                    DataRow _row = _table.NewRow();
                    _row.ItemArray = row.ItemArray;
                    _table.Rows.Add(_row);
                }
            }
            return _table;
        }
View Code

 

DataTable 过滤重复行