Reputation: 4027
I have the following code in C#:
public async Task<List<Table>> GetRows()
{
return await db.Table.ToListAsync();
}
where db is an EF 6 DbContext.
How do I write the equivalent thing in F# assuming that I access the same DbContext?
I came up with this code but I am stuck:
let getRows = async {
let q = query {
from r in db.Table
select r
}
q |> Seq.map(fun row -> ...) // <-- Here I might want to do some custom function
}
Thank you
Upvotes: 3
Views: 316
Reputation: 31799
I generally use TaskBuilder.fs, if most of what I'm doing is going to be using c# tasks. I find this preferable to converting back and forth between Async and Task, but it depends on the apis being used.
add from Nuget
dotnet add package TaskBuilder.fs --version 2.1.0
open and use task
open FSharp.Control.Tasks.V2
task {
let! q = db.Table.ToListAsync()
}
Upvotes: 1
Reputation: 1593
There's no problem in using .ToListAsync()
in F#. I assume that the point of your confusion is to make C# await
play nicely with F# async
.
In this case, you should use Async.AwaitTask
Here's a code
async {
let! q = db.Table.ToListAsync() |> Async.AwaitTask //here you have a C# list
}
Upvotes: 7