Sunday, April 28, 2013

Select unique rows from a DataTable

  1. Logic:

        /// <summary>
        /// Selects the distinct from data table.
    
        /// </summary>
    
        /// <param name="TableName">Name of the table.</param>
    
        /// <param name="SourceTable">The source table.</param>
    
        /// <param name="FieldName">Name of the field.</param>
    
        /// <returns></returns>
    
        public static DataTable DistinctDataFromDataTable(string TableName, DataTable SourceTable, string FieldName)
    
        {
    
            DataTable dt = new DataTable(TableName);
    
            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;
    
        }
    
    
    
        /// <summary>
    
        /// Columns the equal.
    
        /// </summary>
    
        /// <param name="A">The A.</param>
    
        /// <param name="B">The B.</param>
    
        /// <returns></returns>
    
        private static bool ColumnEqual(object A, object B)
    
        {
    
    
    
            // Compares two values to see if they are equal. Also compares DBNULL.Value.
    
            // Note: If your DataTable contains object fields, then you must extend this
    
            // function to handle them in a meaningful way if you intend to group on them.
    
    
    
            if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
    
                return true;
    
            if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
    
                return false;
    
            return (A.Equals(B));  // value type standard comparison
    
        }

    Example:

    public
     static void GetUserNames()  
  2.         {  
  3.             DataTable dt = new DataTable();  
  4.             dt.Columns.Add("UserId"typeof(int));  
  5.             dt.Columns.Add("UserName"typeof(string));  
  6.             dt.Rows.Add(1, "Joe Smith");  
  7.             dt.Rows.Add(2, "John Doe");  
  8.             dt.Rows.Add(3, "Joe Smith");  
  9.             dt.Rows.Add(4, "Jane Smith");  
  10.             dt.Rows.Add(5, "Jane Doe");  
  11.   
  12.             DataTable filterTable = GetUniqueEntries(dt);  
  13.   
  14.         }  
  15.   
  16.   
  17.         /// <summary>  
  18.         /// Gets the unique entries.  
  19.         /// </summary>  
  20.         /// <param name="dt">DataTable</param>  
  21.         /// <returns></returns>  
  22.         private static DataTable GetUniqueEntries(DataTable dt)  
  23.         {  
  24.             var query = (  
  25.             from row in dt.AsEnumerable()  
  26.             select row.Field<string>("UserName")).Distinct();  
  27.   
  28.             DataTable dtDistinctNames = new DataTable();  
  29.             dtDistinctNames.Columns.Add("UserName"typeof(string));  
  30.   
  31.             //have to return a datatable, thus loop through entries  
  32.             foreach (string item in query)  
  33.             {  
  34.                 DataRow newRow = dtDistinctNames.NewRow();  
  35.                 newRow["UserName"] = item;  
  36.                 dtDistinctNames.Rows.Add(newRow);  
  37.             }  
  38.   
  39.             return dtDistinctNames;  
  40.         } 
    This code snippet will return you a list of users where 'username' are unique. There eliminating duplicate names from the list. There are alternative ways to do this type of functionality

No comments:

Post a Comment