2
\$\begingroup\$

This is a follow up to this question.

I'm just removing some columns based on some condition as well using the same condition and adding the column values which I am going to remove to another column. I'm simply performing an addition of columns as well removing them based upon some condition.

I know the below code is too hard to read as well as being un-optimized.

if (!string.IsNullOrEmpty(criteria.DemographicsCategory) && criteria.DemographicsCategory.ToLower() == "income")
{
  DataTable dtresult = results.Results[0].DataTable;
  bool hasValue = false;
  if (dtresult.Columns.Count > 0)
  {
    int rowscount = 0;
    foreach (DataRow row in dtresult.Rows)
    {
      decimal rowSum = 0;
      foreach (DataColumn col in dtresult.Columns)
      {


        if ((col.ColumnName.Equals(income_8B) || (col.ColumnName.Equals(income_8A)) || (col.ColumnName.Equals(income_9)) || (col.ColumnName.Equals(income_10))))
        {
          if (!row.IsNull(col))
          {
            if (col.ColumnName.Equals(income_8A)) hasValue = true;
            string stringValue = row[col].ToString();
            stringValue = stringValue.Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.PercentSymbol, "");
            decimal d;
            if (decimal.TryParse(stringValue, out d))
              rowSum += d;
          }
        }
      }
      rowscount++;
      if (rowscount >= 1 && hasValue)
      {
        row.SetField(income_8A, Convert.ToDouble(rowSum) + "%");
      }
      else
      {
        dtresult.Columns.Remove(income_8A_question);
        dtresult.Columns.Remove(income_8A);
        dtresult.Columns.Remove(income_8A_row1);
        dtresult.Columns.Remove(income_8A_row2);
      }
    }
    if (hasValue)
    {
      dtresult.Columns.Remove(income_8B_question);
      dtresult.Columns.Remove(income_8B);
      dtresult.Columns.Remove(income_8B_row1);
      dtresult.Columns.Remove(income_8B_row2);

      dtresult.Columns.Remove(income_9_question);
      dtresult.Columns.Remove(income_9);
      dtresult.Columns.Remove(income_9_row1);
      dtresult.Columns.Remove(income_9_row2);


      dtresult.Columns.Remove(income_10_question);
      dtresult.Columns.Remove(income_10);
      dtresult.Columns.Remove(income_10_row1);
      dtresult.Columns.Remove(income_10_row2);

      foreach (MultiQuery.ColumnConfig column in results.Results[0].TableConfig.columns.ToList())
      {
        if (column.column_name.Equals(income_9) || column.column_name.Equals(income_9_question)
            || column.column_name.Equals(income_9_row1) || column.column_name.Equals(income_9_row2) ||
            column.column_name.Equals(income_10) || column.column_name.Equals(income_10_question)
            || column.column_name.Equals(income_10_row1) || column.column_name.Equals(income_10_row2) ||
            column.column_name.Equals(income_8B) || column.column_name.Equals(income_8B_question)
            || column.column_name.Equals(income_8B_row1) || column.column_name.Equals(income_8B_row2))
        {
          results.Results[0].TableConfig.columns.Remove(column);
        }
      }

      foreach (MultiQuery.HeaderCell c1 in results.Results[0].TableConfig.display_config.header_rows_web[1].header_cells.ToList())
      {
        if (c1.display_text == "$150,000 - $199,999" || c1.display_text == "$200,000 - $299,999"
            || c1.display_text == "$300,000 & Over")
        {
          results.Results[0].TableConfig.display_config.header_rows_web[1].header_cells.Remove(c1);
        }
      }
      /*For Excel changes -removing header and modifying the column-span*/
      foreach (MultiQuery.HeaderCell c1 in results.Results[0].TableConfig.display_config.header_rows_excel[1].header_cells.ToList())
      {
        if (c1.display_text == "$150,000 - $199,999" || c1.display_text == "$200,000 - $299,999"
            || c1.display_text == "$300,000 & Over")
        {
          results.Results[0].TableConfig.display_config.header_rows_excel[1].header_cells.Remove(c1);
        }
      }

      foreach (MultiQuery.HeaderCell c2 in results.Results[0].TableConfig.display_config.header_rows_excel[0].header_cells.ToList())
      {
        if (c2.display_text == "24e Household Income")
        {
          int span_value = c2.colspan - 3;
          if (span_value > 0)
            c2.colspan = span_value;
          else
            results.Results[0].TableConfig.display_config.header_rows_excel[0].header_cells.Remove(c2);
        }

      }
      /*end*/
    }

  }

}
\$\endgroup\$
  • 1
    \$\begingroup\$ What are you trying to do with this code? There's different ways I could see that you can optimize this, but it's important to know what you're doing here to know which method to use. \$\endgroup\$ – Ken Aug 7 '14 at 17:23
  • \$\begingroup\$ @Kenneth am just removing some columns based on some condition as well using the same condition am adding the column values which am going to remove to another column. Simply performing addition of columns as well removing based upon some condition. \$\endgroup\$ – SivaRajini Aug 7 '14 at 17:26
  • \$\begingroup\$ I am bewildered by the surgery being done on the table. Read this StackOverflow question: Copying data of only a few columns \$\endgroup\$ – radarbob Aug 7 '14 at 18:11
1
\$\begingroup\$

You use a lot of nesting with complex if statements, which complicates the flow of data in your code, which was mentioned in other answers that people have given you. I'm going to update my answer as I see more places to optimize, but for readability you might consider doing following one of my favorite patterns for dealing with lists of objects. I use lists.

In your code you have these columns that you know you want to remove. You can construct a list early on in your code to hold these columns:


var columnsToRemove = new List<MultiQuery.ColumnConfig>();
columnsToRemove.Add(income_8A_question);
...
columnsToRemove.Add(income_10_row2);

Then, later when you want to see if the column you're examining matches one of the columns you want to remove, instead of using a dozen OR statements, you can simplify your code to this:


foreach (MultiQuery.ColumnConfig column in results.Results[0].TableConfig.columns.ToList())
{
    if (columnsToRemove.Contains(column.column_name))
    {
        results.Results[0].TableConfig.columns.Remove(column);
    }
}

I have some other ideas that I'm going to add later, but this should get you started.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.