What is the equivalent of EF C# ToListAsync in F#?

c#-to-f# entity-framework-6 f#

Question

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

1
2
10/1/2019 7:15:20 AM

Accepted Answer

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
}
6
10/1/2019 8:51:49 AM

Popular Answer

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()
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow