C#中去掉表中重复的数据

                   /// <summary>

                   /// 去掉表中重复的数据  int

                   /// </summary>

                   /// <param name="SourceTable">原始表</param>

                   /// <param name="FieldName">重复的字段</param>

                   /// <returns></returns>

                   public DataTable SelectDistinct(DataTable SourceTable, string FieldName)

                   {

                            DataTable dt = new DataTable();

                            for (Int32 i = 0; i < SourceTable.Columns.Count; i++)

                            {

                                     string fieldName=SourceTable.Columns[i].Caption;

                                     dt.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

                            }

 

                            DataRow dataRow = dt.NewRow();

                            foreach (DataRow dr in SourceTable.Select("", FieldName))

                            {

                                     string str=dr[FieldName].ToString();

                                     if(str != string.Empty)

                                     {

                                               if(int.Parse(str)!=0)

                                               {

                                                        if (dataRow == null || !(ColumnEqual(dataRow[FieldName], dr[FieldName])))

                                                        {

                                                                 dataRow=dr;

                                                                 DataRow row = dt.NewRow();

                                                                 for (int i = 0; i < dataRow.ItemArray.Length; i++)

                                                                 {

                                                                           row[i] = dataRow[i];

                                                                 }

                                                                 dt.Rows.Add(row);

                                                        }

                                               }

                                     }

                            }

                            return dt;

                   }

 

/// <summary>

                   /// 去掉表中重复的数据 字段 string

                   /// </summary>

                   /// <param name="SourceTable">原始表</param>

                   /// <param name="FieldName">重复的字段</param>

                   /// <returns></returns>

                   public DataTable SelectDistinctStr(DataTable SourceTable, string FieldName)

                   {

                            DataTable dt = new DataTable();

                            for (Int32 i = 0; i < SourceTable.Columns.Count; i++)

                            {

                                     string fieldName=SourceTable.Columns[i].Caption;

                                     dt.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

                            }

 

                            DataRow dataRow = dt.NewRow();

                            foreach (DataRow dr in SourceTable.Select("", FieldName))

                            {

                                     string str=dr[FieldName].ToString();

                                     if(str != string.Empty)

                                     {

                                               if (dataRow == null || !(ColumnEqual(dataRow[FieldName], dr[FieldName])))

                                               {

                                                        dataRow=dr;

                                                        DataRow row = dt.NewRow();

                                                        for (int i = 0; i < dataRow.ItemArray.Length; i++)

                                                        {

                                                                 row[i] = dataRow[i];

                                                        }

                                                        dt.Rows.Add(row);

                                               }

                                     }

                            }

                            return dt;

                   }

 

 

                   private bool ColumnEqual(object A, object B)

                   {

                            if(A == DBNull.Value && B == DBNull.Value) //     两个都是   DBNull.Value

                                     return true;  

                            if(A == DBNull.Value || B == DBNull.Value) //     只有一个是   DBNull.Value

                                     return false;  

                            return (A.Equals(B));//   正常比较

                   }

 

/// <summary>

                   /// 表中筛选数据

                   /// </summary>

                   /// <param name="SourceTable">原始表</param>

                   /// <param name="FieldName1">表里的字段名</param>

                   /// <param name="FieldName2">筛选的数据</param>

                   /// <returns></returns>

                   public DataTable SelectByField(DataTable SourceTable,string FieldName1, string FieldName2)

                   {

                            DataTable dt = new DataTable();

                            for (Int32 i = 0; i < SourceTable.Columns.Count; i++)

                            {

                                     string fieldName=SourceTable.Columns[i].Caption;

                                     dt.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

                            }

 

                            foreach (DataRow dr in SourceTable.Select(" "+FieldName1+" = '" + FieldName2 + "'"))

                            {

                                     DataRow row = dt.NewRow();

                                     for (int i = 0; i < dr.ItemArray.Length; i++)

                                     {

                                               row[i] = dr[i];

                                     }

                                     dt.Rows.Add(row);

                            }

                            return dt;

                   }

posted on 2014-12-26 13:05  秋天  阅读(3891)  评论(1编辑  收藏  举报

导航