Search code examples
c#neo4jneo4jclient

Cypher Query Issue for Counting Nodes?


Hi there I would like to peform the following Cypher query that counts how many nodes my specified node has a relationship with and I pass in the nodeName in this example.

Here is the query:

START n=node:NameIndex(Name = "Mike")
MATCH (n)-->(x)
RETURN n, count(*)

Here is my attempt:

public IEnumerable<VersionNode> GraphNodeCount(string nodeName)
        {
            GraphQueryLogger.Trace("Now entering GraphNodeCount(string nodeName) Method");

            IEnumerable<VersionNode> queryResult = null;

            clientConnection = graphOperations.GraphGetConnection();


                var query = clientConnection
                    .Cypher
                    .Start(new CypherStartBitWithNodeIndexLookup("n", "NameIndex", "Name", nodeName))
                    .Match("n", "-->", "x")
                    .Return<VersionNode>("n, count(*)");
                queryResult = query.Results.ToList(); 


            return queryResult;
        }

Here is my main method:

    GraphQuery graphQuery = new GraphQuery();
    var query = graphQuery.GraphNodeCount("Mike");
    foreach (var item in query)
    {
        Console.WriteLine(item.Name);
    }

Here is the stacktrace:

at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout)
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks)
   at Neo4jClient.GraphClient.Neo4jClient.IRawGraphClient.ExecuteGetCypherResults[TResult](CypherQuery query)
   at Neo4jClient.Cypher.CypherFluentQuery`1.get_Results()
   at ContentManager_Test.ContentManager_Library.Level_1_API.Graph.Query.GraphQuery.GraphNodeCount(String nodeName)

I get NullReferenceException was unhandled Object reference not set to an instance of an object. What am I missing?


Solution

  • I think you're running a very old version of Neo4jClient. I think this because a) you don't have PDBs and b) it's not blowing up with a nice error about why you can't use commands in your Return calls like that.

    1. Please update Neo4jClient
    2. If it still crashes, post the new stack trace to https://bitbucket.org/readify/neo4jclient/issues/new so we can track and fix the bug

    These days, your query should look like this:

    var queryResults = clientConnection
        .Cypher
        .Start(new {
            n = Node.ByIndexLookup("NameIndex", "Name", nodeName)
        })
        .Match("n-->x")
        .Return(n => new {
            Foo = n.As<VersionNode>(),
            CountOfAllNodes = All.Count()
        })
        .Results
        .ToList(); 
    

    (Note: I've typed that out by hand in the textbox here, so I haven't tested it, but it should be correct.)