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.
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.
I use FSharp.Data.Sql with success http://fsprojects.github.io/SQLProvider/ Seq.executeQueryAsync
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With