I have a DataTable, say pdt, in my code. I just want to select all table["id"] and populate then in an integer array.
I can go ahead with a foreach but I am trying to learn Lambda expressions.
I can’t figure out how to deal with this.
I have tried
List<int> Ids = pdt.Select(row => row["Id"]; return Convert.ToInt32(r));
and
int[] Ids = pdt.Select(row => row["Id"]);
but nothing seems to work. I am pretty sure this is a basic question, help out a newbie please.
If you want an array, you need to use the
ToArray()extension method… but you also want to use theDataTableExtensions.AsEnumerable()extension method to make the data table strongly typed in terms of aDataRowsequence:EDIT: As noted in abatishchev’s answer, an alternative to the explicit cast here would be to use the
Field<T>extension method (inDataRowExtenions):