How to initialize F# list when size is unknown, using while..do loop
- by James Black
I have a function that will parse the results of a DataReader, and I don't know how many items are returned, so I want to use a while..do loop to iterate over the reader, and the outcome should be a list of a certain type.
(fun(reader) ->
[
while reader.Read() do
new CityType(Id=(reader.GetInt32 0), Name=(reader.GetString 1), StateName=(reader.GetString 2))
])
This is what I tried, but the warning I get is:
This expression should have type 'unit', but has type 'CityType'. Use 'ignore' to discard the result of the expression, or 'let'
to bind the result to a name.
So what is the best way to iterate over a DataReader and create a list?