views:

39

answers:

3

I have a datatable containing 10 columns. I want to select only two columns of them.

I am not able to do it using SelectMany Extension Method.

I know how to get it from Linq To DataSet but trying using this extension method.

+2  A: 

SelectMany is the wrong method to try to use. You simply need to use Select and create a new anonymous object:

dataTable.AsEnumerable().Select(r => new { r.Column1, r.Column7 });

SelectMany is used to flatten nested collections.

Justin Niessner
A: 

var s = table.Rows.Cast().Select(....);

Danil
+2  A: 

The SelectMany method is used for flattening a sequence of sequences, not for just selecting a subset of the available columns. Use the Select method instead:

var query = yourDataTable.AsEnumerable()
                         .Select(r => new { Column3 = r[3], Column6 = r[6] });
LukeH