Creating generic method or extension for searching through all the columns in DataTable in C#

Few days ago I worked on a project where database table has more than 80 columns. My client asked to create a search so that it can search through all the columns. So I have decided to create an extension method which will search through all the columns in DataTable. Creating generic method or extension for searching through all the columns in DataTable in C#.

It is difficult to write a SQL query (when table has many columns) which will search through all the columns.

Continue reading

Select Distinct Record from DataTable

If it is necessary to select distinct record from Data Table, then we can use following method.

        public DataTable SelectDistinct(DataTable SourceTable, string FieldName)
        {
            DataTable dt = new DataTable();
            dt = SourceTable.Clone();

            object LastValue = null;
            foreach (DataRow dr in SourceTable.Select("", FieldName))
            {
                if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
                {
                    LastValue = dr[FieldName];
                    dt.ImportRow(dr);
                }
            }
            
 Continue reading