How to find if dataTable contains column which name starts with abc

VilemRousi picture VilemRousi · Sep 3, 2012 · Viewed 10.5k times · Source

In my program I have a dataTable and I´d like to know if is there a column which name starts with abc. For example I have a DataTable and its name is abcdef. I like to find this column using something like this:

DataTable.Columns.Constains(ColumnName.StartWith(abc))

Because I know only part of the column name, I cannot use a Contains method. Is there any simple way how to do that?

Thanks a lot.

Answer

Kirill Bestemyanov picture Kirill Bestemyanov · Sep 3, 2012

You can use this:

 var datatable = new DataTable();
 var abccolumns = datatable.Columns.Cast<DataColumn>()
                                   .Where(c => c.ColumnName.StartsWith("abc"));