EdWood EdWood - 3 months ago 32
C# Question

LINQ C# select multiple columns by names in arrays

Is it possible to select columns by names or indexes, which are stored in array?

String[] columnsWhichINeed = new String[] { "column1", "column2" };

DataColumn[] aCols = dataTable.Columns.Cast<DataColumn>()
.Where(c => c.ColumnName.NEEDSOMEMAGICHERE;
.Select(c => new DataColumn(c.ColumnName, c.DataType))
.ToArray();

Answer

You can probably use Contains() method like

.Where(c => columnsWhichINeed.Contains(c.ColumnName));