Fill an array (or arraylist) from SqlDataReader

It is possible. In .NET 2.0+, SqlDataReader inherits from DbDataReader, which implements IEnumerable (non-generic one). This means that you can use LINQ:

List<string> list = (from IDataRecord r in dataReader
                     select (string)r["FieldName"]
                    ).ToList();

That said, the loop is still there, it’s just hidden in Enumerable.Select, rather than being explicit in your code.

Leave a Comment