日期:2014-05-18 浏览次数:20497 次
private bool ColumnEqual(object A,object B) { if ( A == DBNull.Value && B == DBNull.Value ) return true; if ( A == DBNull.Value || B == DBNull.Value ) return true; return ( A.Equals(B) ); } /// <summary> /// Distinct表字段 /// </summary> /// <param name="SourceTable">DataTable源</param> /// <param name="FieldName">要Distinct的字段</param> /// <returns>返回一个Distinct表,只有一列,就是Distinct列</returns> public DataTable SelectDistinct(DataTable SourceTable, string FieldName) { DataTable dt = new DataTable(); dt.Columns.Add(FieldName,SourceTable.Columns[FieldName].DataType); object LastValue = null; foreach(DataRow dr in SourceTable.Select("",FieldName)) { if ( LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])) ) { LastValue = dr[FieldName]; dt.Rows.Add(new object[]{LastValue}); } } return dt; }