Search code examples
c#neo4jcypherneo4jclient

Use alias defined in a with-clause in a where-clause of a Neo4jclient query


I want to create a query in neo4jclient for this cypher query:

MATCH (price:Item)-->(item:Item)
WHERE (item.Id = 2)
WITH item, max(price.Timestamp) AS maxDate
MATCH (price:Item)-->(item:Item)
WHERE (price.Timestamp = maxDate)
RETURN price

I already came up with this piece of code:

_graphClient.Cypher
        .Match("(price:Price)-->(item:Item)")
        .Where((Item item) => item.Id == 2)
        .With((price, item) => new 
           {
             item, 
             maxDate = Return.As<DateTimeOffset>("max(price.Timestamp)")
           })
        .Match("(price:Price)-->(item:Item)")
        .Where((Price price) => price.Timestamp == Return.As<DateTimeOffset>("maxDate"))
        .Return(price=> price.As<Price>())

but this returns the maxDate alias in qoutes:

MATCH (price:Item)-->(item:Item)
WHERE (item.Id = 2)
WITH item, max(price.Timestamp) AS maxDate
MATCH (price:Item)-->(item:Item)
WHERE (price.Timestamp = "maxDate") <--
RETURN price

What is the correct way to do this?


Solution

  • Code wise the Return.As statements are used to Help Neo4jClient know what to parse the results as (when used in a Return statement) and With is treated in the same way.

    When they're used, they output the Cypher you see (so "max.Timestamp as maxDate"), and Return.As will always output that style of response :/

    The only way I know of to get the Where the way you want is via Where("price.Timetamp = maxDate"), using the string overload.