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;

  • 相关阅读:
    Webstorm 下的Angular2.0开发之路
    利用hexo+github+nodejs搭建自我博客的一天
    滑稽的下午angularjs 2.0管道的使用
    DNGuard 标准版 v2.90发布
    DNGuard Enterprise v2.92 released
    C#复杂表达式的问题
    直接在.Net程序(C#)中执行 native code
    采用Native 引导方式的.Net加密保护
    DNGuard HVM Trial V2.82 发布
    DNGuard Enterprise v2.90 released
  • 原文地址:https://www.cnblogs.com/51net/p/2742351.html
Copyright © 2011-2022 走看看