Skip to content

LINQ to ReQL Provider

bchavez edited this page Jun 10, 2019 · 36 revisions

Yo. So we heard you like LINQ ... so we put some LINQ in your RethinkDB. The C# driver offers an experimental LINQ to ReQL provider as an alternative to ReQL when querying data from RethinkDB. The LINQ to ReQL provider provides a more idiomatic .NET experience when working with RethinkDB. Here's a quick example:

var games = new[]
   {
       new Game {id = 2, Player = "Bob", Points = 15, Type = "ranked"},
       new Game {id = 5, Player = "Alice", Points = 7, Type = "free"},
       new Game {id = 11, Player = "Bob", Points = 10, Type = "free"},
       new Game {id = 12, Player = "Alice", Points = 2, Type = "free"},
   };

//Insert the games above...

// Now, query the games table via LINQ to ReQL
var results = R.Db("lobby").Table<Game>("games", conn)
    .Where(g => g.Type == "free" && g.Points > 5)
    .OrderBy(g => g.Points)
    .ToList();

results.Dump();

/* OUTPUT:
[
  {
    "id": 5,
    "Player": "Alice",
    "Points": 7,
    "Type": "free"
  },
  {
    "id": 11,
    "Player": "Bob",
    "Points": 10,
    "Type": "free"
  }
]
*/

Pretty awesome right? Type-safety all around. So, how does one achieve magical greatness like this? Be a developer hero and continue reading below.

Getting Started

Download & Install

NuGet Package RethinkDb.Driver.Linq

Install-Package RethinkDb.Driver.Linq -Pre

Usage

Currently, the LINQ provider is still in development and considered experimental. Some LINQ methods do not map directly onto ReQL so there will be edge cases where the LINQ provider will throw NotImplementedException. For complex queries that can't be handled by the LINQ provider developers will need to use ReQL directly.

Supported LINQ methods:

  • Any
  • All
  • Average
  • Contains
  • FirstOrDefault
  • First
  • GroupBy
  • LastOrDefault
  • Last
  • OrderBy
  • Where

We will gladly accept PRs for any missing LINQ methods but tread carefully. 😎. LINQ providers have godlike complexity 👼 so any PR in this area will be held to the highest scrutiny. We ask developers to make an issue and let everyone know before anyone attempts to work on any parts of the LINQ provider. Alternatively, ping @bchavez or @jrote1 in the http://slack.rethinkdb.com channel.

Secondary Indexes

There is very limited support for indexes. For example, given the following POCO decorated Game class:

public class Game
{
    public int id { get; set; }
    public string Player { get; set; }

    [SecondaryIndex]
    public int Points { get; set; }
    public string Type { get; set; }
}

and the following query:

// Query games table via LINQ to ReQL
var results = R.Db("lobby").Table<Game>("games", conn)
    .Where(g => g.Points == 10)
    .ToList();

The LINQ provider will use the Points index when equality operators are used in the Where clause. greater than and less than don't work ... yet. 😉