Search code examples
c#neo4jneo4jclient

Method chaining in neo4jclient's return method


I cannot figure out what the neo4jclient equivalent is for the following cypher query:

match (n)
return n.Property, n.AnotherProperty, n.head(label(n))

This is what I have so far:

.Match("n")
.Return((n) => new SomeNode
{
    Property = n.As<SomeNode>.Property,
    AnotherProperty = n.As<SomeNode>.AnotherProperty,
    Label = n.Labels() //This gives all labels, I only need the head. I can't chain it with ".head()"
}

I've looked around on stackoverflow as well as on the wiki but I couldn't find any information on chaining functions.

Is there a way to achieve chaining in the way that I describe here, do I need to construct another query to get labels separately, then take the head of that list and add that to SomeNode, or am I missing a better, completely different approach?

I initially got here when I came across an error stating: "If you want to run client-side logic to reshape your data in .NET, use a Select call after the query has been executed, like .Return(…).Results.Select(r => …)." Examples for this are, however, also not on the wiki.


Solution

  • The best approach that I can quickly think of is to use Return.As for any things like this you want to do in Return, so something like:

    .Match("(n)")
    .Return((n) => new SomeNode
    {
        Property = n.As<SomeNode>.Property,
        AnotherProperty = n.As<SomeNode>.AnotherProperty,
        Label = Return.As<string>("head(labels(n))")
    }
    

    You could use a With:

    .Match("(n)")
    .With("n, head(labels(n)) AS firstLabel")
    .Return((n, firstLabel) => new SomeNode
    {
        Property = n.As<SomeNode>.Property,
        AnotherProperty = n.As<SomeNode>.AnotherProperty,
        Label = firstLabel.As<string>()
    }
    

    Much of a muchness between the two - you have to write the head bit as a string either way :/