O'Reilly logo

Mastering F# by Suhaib Fahad, Alfonso García-Caro Núñez

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using LINQ in F#

F# has query expressions that help to easily build Language Integrated Query (LINQ)  queries. Query expressions can be declared as query { ... }. It is a type of computation expression, just like the sequence expression. An example code is given as follows:

let data = [| 1..10 |] 
 
let simpleExpression() = 
    query { 
        for d in data do 
        select d 
        contains 5 
    } 
> simpleExpression();; 
val it : bool = true 

Query expressions have a list of query operators with which we can use the LINQ-like operations on the data:

 let designations = [| "CEO"; "CTO"; "Manager"; "Employee" |] type Employee = { FirstName: string LastName: string Designation: string Salary: int } with static member DummyData() = let r = new System.Random() seq { for i = 0 to 10 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required