IDataReader.AsSeq

namespace Foo

[<AutoOpen>]
module FsDataEx =

    type System.Data.IDataReader with

        /// Exposes a reader's current result set as seq<IDataRecord>.
        /// Reader is closed when sequence is fully enumerated.
        member this.AsSeq =
            seq { use reader = this
                  while reader.Read() do 
                      yield reader :> IDataRecord }

        /// Exposes all result sets in a reader as seq<seq<IDataRecord>>.
        /// Reader is closed when sequence is fully enumerated.
        member this.AsMultiSeq =
            let rowSeq (reader:IDataReader)  =
                seq { while reader.Read() do yield reader :> IDataRecord }
            seq {
                use reader = this
                yield rowSeq reader
                while reader.NextResult() do
                    yield rowSeq reader
            }
Example:
for row in reader.AsSeq do
    // stuff with row

Description

Exposes a reader's current result set as seq<IDataRecord>. Reader is closed when sequence is fully enumerated. Bonus method: turns an IDataReader with multiple result sets into a seq<seq<IDataRecord>>.

Details

Double click on the code to select all.

 

;