patterncsharpMinor
Modifying a data table of report data
Viewed 0 times
reportdatatablemodifying
Problem
Is there any way of optimizing this code?
```
private void ModifyDataTable(DataTable dt)
{
if (dt != null)
{
bool b_dataValue = false;
decimal rowreport_data0Sum, rowreport_data1Sum, rowreport_data2Sum, rowreport_data3Sum, rowreport_data4Sum;
rowreport_data0Sum = rowreport_data1Sum = rowreport_data2Sum = rowreport_data3Sum = rowreport_data4Sum = 0;
foreach (DataRow row in dt.Rows)
{
if (row.ItemArray[1].ToString() == "$150,000 & Over" || row.ItemArray[1].ToString() == "$150,000 - $199,999" || row.ItemArray[1].ToString() == "$200,000 - $299,999" || row.ItemArray[1].ToString() == "$300,000 & Over")
{
rowreport_data0Sum += GetRowValue(row, "report_data0");
rowreport_data1Sum += GetRowValue(row, "report_data1");
rowreport_data2Sum += GetRowValue(row, "report_data2");
rowreport_data3Sum += GetRowValue(row, "report_data3");
rowreport_data4Sum += GetRowValue(row, "report_data4");
}
}
if (dt.Columns.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
if (row.ItemArray[1].ToString() == "$150,000 & Over")
{
foreach (DataColumn col in dt.Columns)
{
if ((col.ColumnName == "report_data0") || (col.ColumnName == "report_data1") || (col.ColumnName == "report_data2") || (col.ColumnName == "report_data3") || (col.ColumnName == "report_data4"))
{
if (!row.IsNull(col))
{
b_dataValue = true;
if (b_dataValue)
{
if (col.ColumnName == "report_data0") row[col] = rowreport_data0Sum + "%";
else if (col.ColumnName == "report_data1") row[col] = row
```
private void ModifyDataTable(DataTable dt)
{
if (dt != null)
{
bool b_dataValue = false;
decimal rowreport_data0Sum, rowreport_data1Sum, rowreport_data2Sum, rowreport_data3Sum, rowreport_data4Sum;
rowreport_data0Sum = rowreport_data1Sum = rowreport_data2Sum = rowreport_data3Sum = rowreport_data4Sum = 0;
foreach (DataRow row in dt.Rows)
{
if (row.ItemArray[1].ToString() == "$150,000 & Over" || row.ItemArray[1].ToString() == "$150,000 - $199,999" || row.ItemArray[1].ToString() == "$200,000 - $299,999" || row.ItemArray[1].ToString() == "$300,000 & Over")
{
rowreport_data0Sum += GetRowValue(row, "report_data0");
rowreport_data1Sum += GetRowValue(row, "report_data1");
rowreport_data2Sum += GetRowValue(row, "report_data2");
rowreport_data3Sum += GetRowValue(row, "report_data3");
rowreport_data4Sum += GetRowValue(row, "report_data4");
}
}
if (dt.Columns.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
if (row.ItemArray[1].ToString() == "$150,000 & Over")
{
foreach (DataColumn col in dt.Columns)
{
if ((col.ColumnName == "report_data0") || (col.ColumnName == "report_data1") || (col.ColumnName == "report_data2") || (col.ColumnName == "report_data3") || (col.ColumnName == "report_data4"))
{
if (!row.IsNull(col))
{
b_dataValue = true;
if (b_dataValue)
{
if (col.ColumnName == "report_data0") row[col] = rowreport_data0Sum + "%";
else if (col.ColumnName == "report_data1") row[col] = row
Solution
is there any possibility of optimizing the above code
Eliminating unnecessary processing will help...
Remove Needless if
Move If's to the top
If the table is null or has no columns then there's no point in going on. So check for this up front. This removes 2 nesting levels.
Reference by Table Name and Remove more If-ing
I'm guessing that your code is handling at least 2 different tables. If this is true then based on the name call "thisTable" code or "thatTable" code. Then you can eliminate all that checking for column names because you know they are there. And it eliminates another layer of
Eliminating unnecessary processing will help...
Remove Needless if
if (!row.IsNull(col))
{
b_dataValue = true;
if (b_dataValue)
{
// ...
}
else
{
dt.Rows.Remove(row);
}
}b_dataValue is always true, so the if is not necessary, and the else will never execute. So ...if (!row.IsNull(col))
{
b_dataValue = true;
{
// ...
}
}Move If's to the top
If the table is null or has no columns then there's no point in going on. So check for this up front. This removes 2 nesting levels.
private void ModifyDataTable(DataTable dt) {
if (dt == null) return;
if (dt.Columns.Count <= 0) return;
// ...
}Reference by Table Name and Remove more If-ing
I'm guessing that your code is handling at least 2 different tables. If this is true then based on the name call "thisTable" code or "thatTable" code. Then you can eliminate all that checking for column names because you know they are there. And it eliminates another layer of
ifprivate void ModifyDataTable(DataTable dt) {
if (dt == null) return;
if (dt.Columns.Count <= 0) return;
if (dt.TableName == "thisTable") DoThisTableStuff(dt);
if (dt.TableName == "thatTable") DoThatTableStuff(dt);
// Knowing what table we're dealing with, code like this
if (col.ColumnName == "report_data0") row[col] = rowreport_data0Sum + "%";
else if (col.ColumnName == "report_data1") row[col] = rowreport_data1Sum + "%";
// could turn into this
row["report_data0"] = rowreport_data0Sum + "%";
row["report_data1"] = rowreport_data1Sum + "%";
}Code Snippets
if (!row.IsNull(col))
{
b_dataValue = true;
if (b_dataValue)
{
// ...
}
else
{
dt.Rows.Remove(row);
}
}if (!row.IsNull(col))
{
b_dataValue = true;
{
// ...
}
}private void ModifyDataTable(DataTable dt) {
if (dt == null) return;
if (dt.Columns.Count <= 0) return;
// ...
}private void ModifyDataTable(DataTable dt) {
if (dt == null) return;
if (dt.Columns.Count <= 0) return;
if (dt.TableName == "thisTable") DoThisTableStuff(dt);
if (dt.TableName == "thatTable") DoThatTableStuff(dt);
// Knowing what table we're dealing with, code like this
if (col.ColumnName == "report_data0") row[col] = rowreport_data0Sum + "%";
else if (col.ColumnName == "report_data1") row[col] = rowreport_data1Sum + "%";
// could turn into this
row["report_data0"] = rowreport_data0Sum + "%";
row["report_data1"] = rowreport_data1Sum + "%";
}Context
StackExchange Code Review Q#59387, answer score: 8
Revisions (0)
No revisions yet.