The F# 3.0 Freebase Type Provider Sample: Queries for Presidents, Books and Stars

 [ Update: the Freebase type provider is now available as part of the FSharp.Data NuGet package and library. The namespace has changed from “Samples.DataStore.Freebase” to “FSharp.Data”. Find out more about F# at fsharp.org.  ]

Part 1 – The Freebase Type Provider Sample – Integrating Internet-Scale Data Sources into a Strongly Typed Language
Part 2 – The Freebase Type Provider Sample – Static Parameters
Part 3 – The Freebase Type Provider Sample – Sample Queries for Presidents,  Books and Stars
Part 4 – The Freebase Type Provider Sample – Some Info On Queries

The F# 3.0 Freebase Type Provider Sample includes some support for query translation from F# 3.0 LINQ queries to MQL.

This means you can write queries in F# 3.0 with auto-completion and strong typing, and still execute efficiently on the server, at least for the queries translated to MQL 

Here are some sample queries using the F# 3.0 Freebase Type Provider Sample.  These are translated fully to MQL (Note: some require the latest updates to the sample)

    #r@”….Debugnet45Samples.DataStore.Freebase.dll”

   
open Samples.DataStore.Freebase

    let data = Samples.DataStore.Freebase.FreebaseData.GetDataContext()

 

    // Name some sub-domains of data
   
let biology = data.“Science and Technology“.Biology
   
let computers = data.“Science and Technology“.Computers
   
let chemistry = data.“Science and Technology“.Chemistry
   
let astronomy = data.“Science and Technology“.Astronomy
   
let books = data.“Arts and Entertainment“.Books

 

    /// Get the names of the US presidents
   
let presidents =
       
query { for e in data.Society.Government.“US Presidents“ do
               
select e.Name }
       
|> Seq.toList

 

    /// Count the stars listed in the database
   
let numberOfStars = astronomy.Stars.Count()

 

    /// The name and distances of stars which have a distance recorded.
   
let someStarDistances =
       
query { for e in astronomy.Stars do
               
where e.Distance.HasValue
               
select (e.Name, e.Distance) }
         
|> Seq.toList

 

    /// Get the stars in the database sorted by proximity to earth
   
let starsSortedByProximityToEarth =
       
query { for e in astronomy.Stars do
               
sortBy e.Distance.Value
               
take 10
               
select e }
         
|> Seq.toList

 

     /// Get some stars close to Earth
   
let getSomeCloseStars =
       
query { for e in astronomy.Stars do
               
where (e.Distance.Value < 4.011384e+18<_>)
               
select e }
         
|> Seq.toList

 

     /// Get the first 10 books matching a user string.
   
let topBooksWithNameContaining (s:string) =
       
query { for book in data.“Arts and Entertainment“.Books.Books do
               
where (book.Name.ApproximatelyMatches s)
               
take 10
               
select book.Name }

 

    topBooksWithNameContaining “1984” |> Seq.toList

Enjoy!

Don, for The Visual F# Team