I've got a Linq query against a DataRow[] that returns elements from each row. Looks a bit like this:
var query = from row in rows select row["Id"];
I want to convert this into an int[], but the following code generates an error:
int[] myIntArray = query.ToArray();
The error is:
Cannot implicitly convert type object[] to int[]
Is there some clever trick for making this work?
The DataRow
indexer (row[...]
) returns object
, not int
.
Therefore, your implicitly-typed query is a set of object
s.
You need to explicitly select int
s, like this:
int[] numbers = rows.Select(r => r.Field<int>("Id")).ToArray();
(or just select (int)row["Id"]
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With