Search code examples
neo4jneo4jclient

Logical grouping of operator with Neo4jClient


I cant seem to figure out how to write this query using the Neo4jClient

MATCH (n) 
WHERE (n.Id="1ef17d65-492e-4c0d-aa79-13065edd37f2" AND n.CaseType="NaturalPerson") OR (n.Id="a5c143d4-0306-4057-a96f-e39c5c50eb22" AND n.CaseType="NaturalPerson")
RETURN n

If i write the query like this

this.client.Cypher.Match("(n)")
                .Where("f.Id=\"1ef17d65-492e-4c0d-aa79-13065edd37f2\"")
                .AndWhere("n.CaseType=\"NaturalPerson\"")
                .OrWhere("f.Id=\"1ef17d65-492e-4c0d-aa79-13065edd37f2\"")
                .AndWhere("n.CaseType=\"NaturalPerson\"")
                .Return((n, r) => n.As<T>());

The following query is generated, but it does not have the parentheses '(' ')' which logicly group the two and clauses

MATCH (n)
WHERE f.Id="1ef17d65-492e-4c0d-aa79-13065edd37f2"
AND n.CaseType="NaturalPerson"
OR f.Id="1ef17d65-492e-4c0d-aa79-13065edd37f2"
AND n.CaseType="NaturalPerson"
RETURN n AS Node, r AS Metadata

Solution

  • You either do:

    .Where("n.Id='1ef17d65-492e-4c0d-aa79-13065edd37f2' AND n.CaseType='NaturalPerson')
    .OrWhere("n.Id='a5c143d4-0306-4057-a96f-e39c5c50eb22' AND n.CaseType='NaturalPerson'")
    

    or, you could put it all in one .Where

    But, if it was me, I would do:

    .Where("n.CaseType = 'NaturalPerson')
    .AndWhere("n.Id IN ['1ef17d65-492e-4c0d-aa79-13065edd37f2','a5c143d4-0306-4057-a96f-e39c5c50eb22']
    

    And I would definitely be using parameters like so:

    var ids = new [] {'a5c143d4-0306-4057-a96f-e39c5c50eb22', '1ef17d65-492e-4c0d-aa79-13065edd37f2'};
    var caseType = "NaturalPerson";
    
    this.client.Cypher.Match("(n)")
        .Where("n.CaseType = $caseTypeParam")
        .AndWhere("n.Id IN $idsParam")
        .WithParam("caseTypeParam", caseType)
        .WithParam("idsParam", ids")
        /*.. etc..*/