根据字段去除dt重复列

  ///    
        /// 按照fieldName从sourceTable中选择出不重复的行,   
        /// 并且返回sourceTable中所有的列。   
        ///    
        /// 2015-07-27 gaobin
        /// 表名   
        /// 源表   
        /// 字段   
        /// 一个新的不含重复行的DataTable   
        public static DataTable DistinctSomeColumn(DataTable sourceTable, params string[] fieldName)
        {
            DataTable dt2 = sourceTable.Clone();
            DataView v1 = dt2.DefaultView;
            StringBuilder filter = new StringBuilder();
            foreach (DataRow row in sourceTable.Rows)
            {
                for (int i = 0; i < fieldName.Length; i++)
                {
                    filter.AppendFormat("{0}='{1}'", fieldName[i], row[fieldName[i]].ToString().TrimEnd());
                    if (i < fieldName.Length - 1)
                    {
                        filter.Append(" and ");
                    }
                }

                v1.RowFilter = filter.ToString();

                if (v1.Count > 0)
                {
                    filter = new StringBuilder();
                    continue;
                }
                dt2.Rows.Add(row.ItemArray);
                filter = new StringBuilder();
            }
            return dt2;
        }

 

你可能感兴趣的:(.net)