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;

  • 相关阅读:
    javascript 回调函数定义 模板
    获得最近一天的提交,并使用winscp上传到服务器
    virltualbox 升级之后 苹果虚拟机报The installed support driver doesn't match the version of the user解决方案
    ESP-EYE V2.1 开发板 WINDOWS 10 开发入门
    centos 安装gitee备忘
    Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
    requirejs amd module load example
    js object template
    php 基础代码大全(不断完善中)
    自动化web前端测试,自动登录网站.目前发现最靠谱的方法是imacros
  • 原文地址:https://www.cnblogs.com/51net/p/2742351.html
Copyright © 2011-2022 走看看