去除DataTable中重复字段

简介:      #region  去除datatable中重复字段        ///         ///         ///         ///         ///         protected DataTable SelectDistinct(DataTable Sour...

 

     #region  去除datatable中重复字段
         ///   <summary>
        
///   </summary>
        
///   <param name="SourceTable"></param>
        
///   <param name="FieldNames"></param>
        
///   <returns></returns>
         protected DataTable SelectDistinct(DataTable SourceTable,  params  string[] FieldNames)
        {
             object[] lastValues;
            DataTable newTable;
            DataRow[] orderedRows;

             // if (FieldNames == null || FieldNames.Length == 0)
            
//     throw new ArgumentNullException("FieldNames");

            
// 如果FieldNames为空,则默认查询所有的列。
             if (FieldNames ==  null || FieldNames.Length ==  0)
            {
                FieldNames =  new  string[SourceTable.Columns.Count];
                 for ( int i =  0; i < SourceTable.Columns.Count; i++)
                {
                    FieldNames[i] = SourceTable.Columns[i].ColumnName;
                }
            }
           
            lastValues =  new  object[FieldNames.Length];
            newTable =  new DataTable();

             foreach ( string fieldName  in FieldNames)
                newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

            orderedRows = SourceTable.Select( ""string.Join( " , ", FieldNames));

             foreach (DataRow row  in orderedRows)
            {
                 if (!fieldValuesAreEqual(lastValues, row, FieldNames))
                {
                    newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

                    setLastValues(lastValues, row, FieldNames);
                }
            }
             return newTable;
        }
         private  bool fieldValuesAreEqual( object[] lastValues, DataRow currentRow,  string[] fieldNames)
        {
             bool areEqual =  true;

             for ( int i =  0; i < fieldNames.Length; i++)
            {
                 if (lastValues[i] ==  null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
                {
                    areEqual =  false;
                     break;
                }
            }

             return areEqual;
        }
         private DataRow createRowClone(DataRow sourceRow, DataRow newRow,  string[] fieldNames)
        {
             foreach ( string field  in fieldNames)
                newRow[field] = sourceRow[field];

             return newRow;
        }
         private  void setLastValues( object[] lastValues, DataRow sourceRow,  string[] fieldNames)
        {
             for ( int i =  0; i < fieldNames.Length; i++)
                lastValues[i] = sourceRow[fieldNames[i]];
        }
         #endregion

 

 


 

调用如下:

               DataTable tbl = new DataTable();
                tbl.Columns.Add("Id", typeof(System.Int32));
                tbl.Columns.Add("City", typeof(System.String));
                tbl.Columns.Add("Province", typeof(System.String));

                tbl.Rows.Add(1, "武汉", "湖北");
                tbl.Rows.Add(2, "应城", "湖北");
                tbl.Rows.Add(3, "武汉", "湖北");

               // DataTable dt2 = SelectDistinct(tbl, null);
                DataTable dt2 = SelectDistinct(tbl,  "City", "Province" );
                //DataTable dt2 = SelectDistinct(tbl, new string[] { "City", "Province" });
                this.dataGridView1.DataSource = dt2;

目录
相关文章
|
Java
【Java系列】List数据去重的五种有效方法
目录 1:使用java8新特性stream进行List去重 (distinct()方法) 2,借助Set的特性进行去重(set和list转换去重) 3,遍历List集合,将元素添加到另一个List集合中 4,利用set集合特性保持顺序一致去重 5,使用list自身方法remove()–&gt;不推荐 1:使用java8新特性stream进行List去重 (distinct()方法) public static List&lt;String&gt; delRepeat(Lis.
412 0
【Java系列】List数据去重的五种有效方法
|
C#
List<object>进行Distinct()去重
原文:List进行Distinct()去重 有时我们会对一个list集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来。如果list中的T是个自定义对象时直接对集合Distinct是达不到去重的效果。
2406 0
|
2天前
|
弹性计算 运维 搜索推荐
三翼鸟携手阿里云ECS g9i:智慧家庭场景的效能革命与未来生活新范式
三翼鸟是海尔智家旗下全球首个智慧家庭场景品牌,致力于提供覆盖衣、食、住、娱的一站式全场景解决方案。截至2025年,服务近1亿家庭,连接设备超5000万台。面对高并发、低延迟与稳定性挑战,全面升级为阿里云ECS g9i实例,实现连接能力提升40%、故障率下降90%、响应速度提升至120ms以内,成本降低20%,推动智慧家庭体验全面跃迁。
|
3天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
351 91
|
10天前
|
人工智能 自然语言处理 前端开发
Qoder全栈开发实战指南:开启AI驱动的下一代编程范式
Qoder是阿里巴巴于2025年发布的AI编程平台,首创“智能代理式编程”,支持自然语言驱动的全栈开发。通过仓库级理解、多智能体协同与云端沙箱执行,实现从需求到上线的端到端自动化,大幅提升研发效率,重塑程序员角色,引领AI原生开发新范式。
851 156
|
3天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
257 156
|
4天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~