0
\$\begingroup\$

I would like to know if there are ways to make this run faster. Not a big concern right now, but I would like to think long term, it might be important.

This is C#, and I can not change the input parameters data types, but basically I have to make something like the SQL: SELECT returnCol FROM dt WHERE ColName = ColValue

As the datatable itself: it has more than 100 columns, but it should not too many rows, usually ~10 rows but it can spike up to 100 rows sometimes. This is part of a data parse that is happening once a minute, that is why I am trying to see if there are ways for enhancement.

private string FindInT(DataTable dt, string ColName, string ColValue, string returnCol)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row[ColName].ToString().ToLower().Trim() == ColValue.ToLower().Trim())
                {
                    return row[returnCol].ToString();
                }
            }
            return "";
        }
\$\endgroup\$
2
  • 2
    \$\begingroup\$ There is absolutely nothing to worry about. With only 10 rows and 100 columns you will not even be able to measure the difference with a profiler. \$\endgroup\$
    – t3chb0t
    Commented Oct 22, 2018 at 15:25
  • 1
    \$\begingroup\$ it might be important, but it might well not be. I wouldn't worry, as @t3chb0t said you're dealing with a tiny data set: before the performance of this becomes the bottleneck, there are probably other parts of your code that will need to be revisited. The alternatives (rewriting it with LINQ, using a RowFilter on DataView, or calling DataTable.Select()) are likely to be slower - but don't take my word for it: benchmark. This sounds like a case of premature optimization. \$\endgroup\$
    – s.m.
    Commented Oct 22, 2018 at 15:39

1 Answer 1

4
\$\begingroup\$

I have two suggestions:

  1. Trim ColValue once rather than every iteration through the loop.
  2. Use a case-insensitive string compare rather than performing .ToLower() on two different strings on every loop iteration.

Result:

private string FindInT(DataTable dt, string ColName, string ColValue, string returnCol)
{
    ColValue = ColValue.Trim();
    foreach (DataRow row in dt.Rows)
    {
        if (string.Equals(
            row[ColName].ToString().Trim(),
            ColValue,
            StringComparison.InvariantCultureIgnoreCase))
        {
            return row[returnCol].ToString();
        }
    }
    return "";
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Why the Cast<DataRow>() method call? Why not just foreach over the Rows property of the DataTable? \$\endgroup\$ Commented Oct 22, 2018 at 17:02
  • 1
    \$\begingroup\$ @GregBurghardt you found a victim of my copy/pasting. It can and will go away. \$\endgroup\$ Commented Oct 22, 2018 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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