Skip to content

LINQ to ReQL Provider

Brian Chavez edited this page May 13, 2016 · 36 revisions

Yo. So we heard you like LINQ ... so we put some LINQ in your RethinkDB ReQL. The C# driver offers an experimental LINQ to ReQL driver for those who are looking for a more idiomatic .NET experience when querying data from 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 some games
R.Db(DbName)
   .Table(TableName)
   .Insert(games)
   .RunResult(conn)
   .AssertInserted(4);

// Query games table via LINQ to ReQL
var results = R.Db(DbName).Table<Game>(TableName, 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? 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 into ReQL so there will be edge cases where the LINQ provider will throw NotImplementedException. For complex queries that can't be handled by 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(DbName).Table<Game>(TableName, 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 won't work ... yet. 😉