Neo4jClient with LinqPad

Quite often I want to just try out some code, usually useful when answering a question on StackOverflow, but typically these don’t require a full solution, and it’s a bit heavy handed to whip out Visual Studio for a simple create / match query, so what about LinqPad? One of the most awesome tools for a .NET developer (if you haven’t got it, you should get it).

I’m going to assume a few things here:

  1. You have got Neo4j running and know the url (if you’ve changed it)
  2. You have downloaded and installed / have LinqPad > 4.51.01 (at the time of writing at the beta page: http://www.linqpad.net/beta.aspx) running

Set up LinqPad

You need to have version 4.51.01 or greater to be able to download the nuget packages (as this version uses the latest Nuget Client)

With LinqPad open, press F4 (or go to Query, Query Properties):

image

In the query properties window, press the ‘Add NuGet…’ button:

image

Search for ‘Neo4jClient

image

Press ‘Add to Query’, and it’ll start downloading:

image

Accept the license message that pops up:

image

Now you should have the Neo4jClient package added to your local cache:

image

Lastly, we can add the namespaces we’ll want:

image

Select ‘Neo4jClient’ and ‘Neo4jClient.Cypher’:

image

Then press OK

Our query properties should now contain the Neo4jClient nuget package:

image

and the namespaces:

image

You can just press OK now, and it’ll be set up for this query, or press the ‘Set as default for new queries’ button to always have Neo4jClient goodness available.

Querying

Every query will need to begin with the same bit of code, which we’re familiar with:

var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

After that, you can do what you want:

client.Cypher
        .Create("(n:Test {user})")
        .WithParam("user", new {Name = "Chris"})
        .ExecuteWithoutResults();

var results = client.Cypher
                .Match("(n:Test)")
                .Return(n => new {NameReturned = Return.As<string>("n.Name")})
                .Results;
                

And because we’re in LinqPad world, we can just call ‘Dump()’ to get the results:

foreach (var result in results)
    result.Dump();

image

Awesome!

Notes

One thing to note is that the ‘Language’ choice is important, if you’re wanting to use actual classes (so for example, to do ‘n.As<T>()’ instead) you’ll need to have language set to: ‘C# Program’ (or VB, or even F# if you’re up for it!), for all other queries (like the one above) you’ll need to be on ‘C#/VB Statements(s)’ as you need to initialise the client first. ‘C#/VB Expression’ won’t work.

Print | posted @ Tuesday, June 17, 2014 10:18 AM

Comments on this entry:

No comments posted yet.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: