Best practice to check if DataRow contains a certain column

You can create an extension method to make it cleaner:

static class DataRowExtensions
{
    public static object GetValue(this DataRow row, string column)
    {
        return row.Table.Columns.Contains(column) ? row[column] : null;
    }
}

Now call it like below:

foreach(DataRow row in table)
    return yield new Thingy { Name = row.GetValue("hazaa") };

As your DataTable table always has the same columns ( they won`t change for any row ) you only need to check for the columnname once.

if (table.Columns.Contains("donkey"))
{
    foreach ...
}

Tags:

C#

Dataset