Async database query

f#, f#-3.0

Solution

The answer will depend on what you're querying. Many data sources will expose something like a data context that enables running queries in different ways, but this isn't exposed directly on the `IQueryable` type (and is therefore not something that you can do directly with the result of a `query { ... }` expression.

As an example, if your `foo` is a LINQ-to-SQL `Table<Foo>`, then you can do something like this:

let queryExample name =
    let q = 
        query { for f in foo do
                where (f.name = name)
                select f.name }
    let cmd = foo.Context.GetCommand(q)
    async {
        let! reader = Async.AwaitTask (cmd.ExecuteReaderAsync())
        return foo.Context.Translate<Foo>(reader)
    }

This will return an `Async<seq<Foo>>`. If you'll be running lots of queries like this, then it's easy to extract the meat of this into a reusable mechanism.

Problem

I'm just starting out with F# and .Net but after some Googling I didn't find examples of this. I apologize in advance if this is too simple. I'm trying to query a database and do it asynchronously. For example, I have a function like so: ``` let queryExample name = query {for f in foo do where (f.name = name) select f.name} |> Seq.toList ``` Now, how would I make an async version of this? `query` doesn't return an `Async<'a>` type.

Original source