How to initialize F# list when size is unknown, using while..do loop

Posted by James Black on Stack Overflow See other posts from Stack Overflow or by James Black
Published on 2010-05-24T02:32:12Z Indexed on 2010/05/24 2:41 UTC
Read the original article Hit count: 256

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?

© Stack Overflow or respective owner

Related posts about F#

Related posts about functional-programming