HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Generating a table of differences between two dictionaries

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
differencesgeneratingbetweentwodictionariestable

Problem

for (int i = 0; i " + colNames[keyList[i].ToString()].ToString() + "" + oldValue + "" + newValue + "";
        }
    }
    else
    {
        isChnagedSectionFields = "Yes";
        string newValues = newDic[keyList[i].ToString()].ToString();
        table = table + "" + colNames[keyList[i].ToString()].ToString() + "" + " " + "" + newValues + "";
    }
}

Solution

You should separate the logic from your representation and first collect those differences in a separate list. Also, what are the types of your dictionaries? All those ToString()s make it really noisy and are likely not necessary. Furthermore, use a foreach loop to iterate through your key list, this will also automatically get rid of all those keyList[i] accesses. And finally, instead of appending to a string directly, use a StringBuilder to make it more efficient:

// Use a separate type to collect your diffs
class DiffEntry
{
    public string ColumnName { get; set; }
    public string OldValue { get; set; }
    public string NewValue { get; set; }
}

// collect all diffs
List diffs = new List();
foreach (string key in keyList)
{
    if (oldDic.ContainsKey(key))
    {
        if (newDic[key] != oldDic[key])
        {
            diffs.Add(new DiffEntry()
            {
                ColumnName = colNames[key],
                OldValue = oldDic[key],
                newValue = newDic[key]
            });
        }
    }
    else
    {
        diffs.Add(new DiffEntry()
        {
            ColumnName = newDic,
            NewValue = newDic[key]
        });
    }
}

// generate the actual table output
StringBuilder tableContent = new StringBuilder();
foreach (DiffEntry diff in diffs)
{
    tableContent.Append("");
    tableContent.Append("");
    tableContent.Append(diff.ColumnName);
    tableContent.Append("");
    tableContent.Append(diff.OldValue);
    tableContent.Append("");
    tableContent.Append(diff.NewValue);
    tableContent.Append("");
}
table += tableContent.ToString();

Code Snippets

// Use a separate type to collect your diffs
class DiffEntry
{
    public string ColumnName { get; set; }
    public string OldValue { get; set; }
    public string NewValue { get; set; }
}

// collect all diffs
List<DiffEntry> diffs = new List<DiffEntry>();
foreach (string key in keyList)
{
    if (oldDic.ContainsKey(key))
    {
        if (newDic[key] != oldDic[key])
        {
            diffs.Add(new DiffEntry()
            {
                ColumnName = colNames[key],
                OldValue = oldDic[key],
                newValue = newDic[key]
            });
        }
    }
    else
    {
        diffs.Add(new DiffEntry()
        {
            ColumnName = newDic,
            NewValue = newDic[key]
        });
    }
}

// generate the actual table output
StringBuilder tableContent = new StringBuilder();
foreach (DiffEntry diff in diffs)
{
    tableContent.Append("<tr style='border: 1px solid black;'>");
    tableContent.Append("<td style='border: 1px solid black;'>");
    tableContent.Append(diff.ColumnName);
    tableContent.Append("</td><td style='border: 1px solid black;'>");
    tableContent.Append(diff.OldValue);
    tableContent.Append("</td><td style='border: 1px solid black;'>");
    tableContent.Append(diff.NewValue);
    tableContent.Append("</td></tr>");
}
table += tableContent.ToString();

Context

StackExchange Code Review Q#91037, answer score: 2

Revisions (0)

No revisions yet.