Skip to content

LINQ to ReQL Provider

bchavez edited this page May 13, 2016 · 36 revisions

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? 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 the 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 (high-quality, low-maintenance code that is). 😎

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. 😉

Clone this wiki locally