Applications slow down when getting distinct of 50K+ records

Oct 3 2016 1:05 AM
I am having Application slowness when i suppose to get distinct out a DataTable which has 50k+ records. i am using the below code to do it
  1. /// <summary>  
  2.    /// Generate Distinct Data Table from the Data Table Generated from server.  
  3.    /// </summary>  
  4.    Func<CancellationTokenSource, DataGridCollectionView, DataTable, DataTable> GetDistinctTable =  
  5.        (cancellationToken, DqmDataGridCollectionView, ResultDataTable) =>  
  6.        {  
  7.    
  8.            var distinctCol = DqmDataGridCollectionView.SortDescriptions.ToList();  
  9.            if (distinctCol.Any())  
  10.            {  
  11.    
  12.                var _distinctColumns = from n in distinctCol  
  13.                                       select new { col = n.PropertyName.Split('_')[1] }.col;  
  14.    
  15.                var dtLinq20 = new DataTable();  
  16.    
  17.                foreach (var item in _distinctColumns)  
  18.                {  
  19.                    var existingColumn = ResultDataTable.Columns[item];  
  20.                    var _dColumn = new DataColumn { ColumnName = existingColumn.ColumnName, DataType = existingColumn.DataType, DefaultValue = existingColumn.DefaultValue, AllowDBNull = existingColumn.AllowDBNull, Caption = existingColumn.Caption };  
  21.                    dtLinq20.Columns.Add(_dColumn);  
  22.                }  
  23.    
  24.                DataTable DistinctDataTable = null;  
  25.    
  26.                try  
  27.                {  
  28.                    DistinctDataTable = ResultDataTable.AsEnumerable().AsParallel().WithCancellation(cancellationToken.Token)  
  29.                     .Select(row =>  
  30.                     {  
  31.                         var newRow = dtLinq20.NewRow();  
  32.                         foreach (var item in _distinctColumns)  
  33.                         {  
  34.                             newRow[item] = row[item];  
  35.                         }  
  36.    
  37.                         return newRow;  
  38.                     })  
  39.                     .Distinct(DataRowComparer.Default).CopyToDataTable();  
  40.                }  
  41.                catch (Exception)  
  42.                {  
  43.                    return null;  
  44.                }  
  45.    
  46.   
  47.                DistinctDataTable.TableName = "Distinct_TBL";  
  48.    
  49.                return DistinctDataTable;  
  50.            }  
  51.            return null;  
  52.        };  
But for me it's taking too much of my applications load time. can any one suggest me a best solution? 

Answers (7)