Returning List<int> from Database

Simon Kiely picture Simon Kiely · Jun 19, 2012 · Viewed 7.7k times · Source

I have a simple problem that I have not been able to find an answer to despite much Googling. Perhaps posting this question up will help the next poor soul who looks for the same answer.

I have a query that returns a collection of distinct ID's.

I am creating an SQLCommand with this query and using ExecuteScalar( ) to run it.

ExecuteScalar( ) appears to return an object and will not allow me to cast it to List.

What is the best practice to fix this and how can I fix this problem?

Answer

Jason Meckley picture Jason Meckley · Jun 19, 2012

execute scalar returns a single piece of data. think one row, one column. if you want to return a collection you need to call ExecuteReader(). this will return an IDataReader. you can then iterate over the records and return you list.