Getting the values from a Datatable column

I couldn't find any better, reusable way to get all the values from a specific column of a datatable, strongly typed than the snippet below:

        internal class DataColumnConverter<T>
            internal T[] GetValuesAsArray(DataTable data, string columnName)
                List<T> values = new List<T>();
                foreach (DataRow r in data.Rows)
                return values.ToArray();

Things it's missing:

  • Type Cast checking on the type of items in the DataTables' column that's specified
  • Checking that the column specified exists
  • Handling of a column which contains nullable values (Could <T> be passed as, for example int? to cater for this? maybe! Or perhaps null's could be stripped out...)

No Comments

Add a Comment