Translation of Linq into FSharp expression

DocumentDB has a project, albeit an example, in C # that I work in FSharp. One of the first tasks is to find an existing database. C # code is

var database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").ToArray().FirstOrDefault();

I am trying to make the same line in FSharp, but I am not getting. Even if I reference the same libraries. Instead, I get the following:

enter image description here

I think the problem is wrong? Thanks in advance.

+4
source share
1 answer

Linq does not apply to C #. Even in C # you need to add a link to System.Linq.dlland using System.Linq;. C # project templates already include these instructions.

In F # you need to do the same, make sure your project has a link to System.Linq and adds a statement open System.Linq

There are at least two more idiomatic methods:

  • Seq , , :

    let random = new System.Random()
    Seq.initInfinite (fun _ -> random.Next())
    |> Seq.filter (fun x -> x % 2 = 0)
    |> Seq.take 5
    |> Seq.iter (fun elem -> printf "%d " elem)
    printfn ""
    

    seq<'T> IEnumerable<T>, , IQueryable, .

  • Query Expressions, LINQ SQL:

    let countOfStudents =
        query {
            for student in db.Student do
            select student
            count
        }
    

    query IQueryable<T>

:

let database =  
    query {
        for db in client.CreateDatabaseQuery()
        where db.Id == "FamilyRegistry"
        select db
        headOrDefault
    }
+14

All Articles