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

How do I use Structr to iterate through elements in a cypher collection?
Analogue of #Formula annotation from JPA for Spring data neo4j?
Cypher Query does not work
SDN4 prevent Cypher injection(like SQL injection) [duplicate]
Using undirectional Relation query patterns
Cypher Query with Match
expression type add not support in C# windows application
Cypher relationships disconnected from nodes (pointing to nothing). Variable names wrong?
Multiple cypher MATCH queries to get row count
cypher sum properties of a specific relation
Return only one Boolean Row in Cypher instead of Null
How to avoid duplications in union query or is there any other way to do
How do I do a WHERE clause on a COUNT in Cypher?
Cypher: WITH is required between CREATE and MATCH
Cypher - Maximum Connected Common Subgraph
loading the csv file via https, Cypher error

Categories

HOME
firebase
freeradius
clearcase
yii
opencv-python
softlayer
stacktrace.js
odata
semantic-web
twitter-fabric
portable-class-library
mirc
liferay-7
haxe
sentry
ui-automation
native-base
timber
hspi
siesta
yeoman-generator-angular
cqrs
fbloginview
amazon-elb
dhcp
sonata
global-variables
rest-assured
office-ui-fabric
uislider
restier
software-packaging
window-managers
ibm-connections
substance
mapnik
rust-cargo
advanced-filter
payeezy
numerics
landscape
django-smart-selects
jqgrid-asp.net
liteide
tinyioc
rm
deployd
redisson
watir-webdriver
line-intersection
network-protocols
hmmlearn
openerp-6
cognos-tm1
nitrousio
rational-performance-test
google-identity-toolkit
xmlbeans
ftp-client
mathml
maven-release-plugin
apple-configurator
jeditable
jemdoc
i2b2
android-expansion-files
android-mapview
extend
enaml
google-gdk
ipod
shoes
signed
python-c-extension
hp-idol-ondemand
zend-mail
tabris
squeezebox
otl
windows-messages
ivalueconverter
mod-perl
iosched
cloo
ggts
sql-view
ckeditor.net
onconfigurationchanged
live-wallpaper
taskkill
autosize
fork-join
kohana-auth
mygeneration
explicit
outlook-2007-addin
tessellation
ixmldomdocument
source-code-protection
tacit-programming
comment-conventions
gui-designer

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App