Search code examples
c#neo4jneo4jclient

Count records is always 1


I'm trying to get a count of the records that exist in the DB with a certain name and I am trying to use this code:

            query = client.Cypher
                .Match("(n { Name: 'a'})")
                .Return(n => n.Count())
                .Results.Count();

It's looking for a user with a name of 'a'. The problem I have is that if the database has no records or if there is a record with the name of 'a', I always get 1 as the result of the code. I would expect to have 0 when there are no records in the database.


Solution

  • It looks like the last Count() is returning the number of elements returned by .Return(n => n.Count(), which will always be 1 element (a single number).

    Try removing .Count() from the end (and replacing it with Single()):

    query = client.Cypher
                  .Match("(n { Name: 'a'})")
                  .Return(n => n.Count())
                  .Results
                  .Single();