zoukankan      html  css  js  c++  java
  • 去除DataTable中重复字段

        #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;

  • 相关阅读:
    教你如何在Drcom下使用路由器上校园网(以广东工业大学、极路由1S HC5661A为例)
    selenium跳过webdriver检测并模拟登录淘宝
    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口
    使用xib制作界面有时会出现button无法点击,解决办法
    1.开博客的第一篇
    6.关于瀑布模型
    3.Freshman阶段学习内容的确定
    7.Solution的Build、Rebuild和Clean
    4.词法结构JavaScript权威指南笔记
    8.对于.NET的初步理解和介绍
  • 原文地址:https://www.cnblogs.com/51net/p/2742351.html
Copyright © 2011-2022 走看看