cypher


Returning composite class with Neo4jClient


The Neop4jClient cypher wiki (https://github.com/Readify/Neo4jClient/wiki/cypher) contains an example of using lambda expressions to return multiple projections...
var query = client
.Cypher
.Start(new { root = client.RootNode })
.Match("root-[:HAS_BOOK]->book-[:PUBLISHED_BY]->publisher")
.Return((book, publisher) => new {
Book = book.As<Book>(),
Publisher = publisher.As<Publisher>(),
});
So the query will return details of both book nodes and publisher nodes. But I want to do something slightly different. I want to combine the contents of a single node type with a property of the matched path. Lets say I have Person nodes with a property 'name', and a class defined so,,,
public class descendant
{
public string name { get; set; }
public int depth { get; set; }
}
A cypher query like this will return what I want, which is all descendants of a given node with the depth of the relationship...
match p=(n:Person)<-[*]-(child:Person)
where n.name='George'
return distinct child.name as name, length(p) as depth
If I try a Neo4jClient query like this...
var query =
_graphClient.Cypher
.Match("p=(n:Person)<-[*]-(child:Person)")
.Where("n.name='George'")
.Return<descendant>("child.name, length(p)") ;
I get an error that the syntax is obsolete, but I can't figure out how should I project the cypher results onto my C# POCO. Any ideas anyone?
The query should look like this:
var query =
_graphClient.Cypher
.Match("p=(n:Person)<-[*]-(child:Person)")
.Where((Person n) => n.name == "George")
.Return((n,p) => new descendant
{
name = n.As<Person>().Name,
depth = p.Length()
});
The Return statement should have the 2 parameters you care about (in this case n and p) and project them via the lambda syntax (=>) to create a new descendant instance.
The main point this differs from the example, is that the example creates a new anonymous type, whereas you want to create a concrete type.
We then use the property initializer (code inside the { } braces) to set the name and depth, using the As<> and Length extension methods to get the values you want.
As a side note, I've also changed the Where clause to use parameters, you should always do this if you can, it will make your queries both faster and safer.

Related Links

Neo4J 2.0 How to refactor the database to introduce Date nodes
How to get distinct node on condition Cypher-Neo4j
cypher help - create or update a node
Neo4j Configuration for 4M Nodes 10M relationship
Parametrizing relationship depth Neo4jphp
Neo4j - multiple tasks with OPTIONAL MATCH
Returning composite class with Neo4jClient
Spring data neo4j cypher query
String data neo4j cypher query by passing DESC or ASC as a parameter
Spring data neo4j Index search
linear programming on cypher analysis
Neo4J 2.0 M4 - Create unique node with label and properties
Cypher - Issue with Where + Aggregate + With
create relationships between nodes in parallel
How to get exact node based on some property
Using Neo4jclient to search for property with regex

Categories

HOME
qt
android-espresso
android-studio
vagrant
clearcase
opencv-python
exchange-server
histogram
fogbugz
sentry
javafx-8
firebase-dynamic-links
angular2-template
specifications
selection
mutation-testing
gtk
yeoman-generator-angular
rangy
ex
outlook-restapi
corona
heap-dump
l20n
fosuserbundle
amazon-mws
libgit2
animated-gif
uri
crud
offline-caching
android-cardview
msdeploy
uilocalnotification
math.js
django-import-export
cube
amazon-iam
location-services
android-collapsingtoolbar
payeezy
express-session
cordys-opentext
strapi
unification
iscroll
export-to-pdf
httphandler
type-inference
nuclio
xcode8.2
rightnow-crm
cloudwatch
ol3-google-maps
comm
node-apn
sequence-diagram
algebra
mongodb-aggregation
zend-server
prefetch
oid
controllers
researchkit
pjax
bilinear-interpolation
msbuild-4.0
traversable
jenkins-workflow
fiber
fdt
mercurial-extension
bayesglm
directx-9
chicagoboss
bjam
angular-amd
mutators
createfile
cout
storing-data
relationships
quantify
django-sites
codahale-metrics
ui-design
oembed
ntlmv2
arrow
border-box
system-information
autosize
background-repeat
expression-web
powergui
sqlsitemapprovider
index.dat
code-camp
word-automation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App