1
votes

I'm very new to Neo4j, Neo4jClient and Cypher Query. I'm implementing Graphity Data Models and Algorithms. So, i have my own egonetwork on my Neo4j Graph Database. Here is my Cypher Query i'm trying to retrieve egonetwork of a specified user.

MATCH(user:User {userID : '1'})-[:ego1*]->(friend)-[:LATEST_ACTIVITY]->(latest_activity)-[:NEXT*]->(next_activity)
RETURN friend, latest_activity, next_activity

Here is the result: enter image description here So, you see, the order is : 2 3 4 (userID)

And here is my converted code with Neo4jClient

var query = graphClient.Cypher.Match("(user:User {userID : '1'})-[:ego1*]->(friend)-[:LATEST_ACTIVITY]->(latest_activity)-[:NEXT*]->(next_activity)").
                Return((friend, latest_activity, next_activity) => new
                {
                    friends = friend.As<User>(),
                    latest_activity = latest_activity.As<Activity>(),
                    next_activities = next_activity.CollectAs<Activity>()
                }).Results;
        List<User> friendList = new List<User>();
        List<List<Activity>> activities = new List<List<Activity>>();
        List<Activity> activity, tmp;
        foreach (var item in query)
        {
            friendList.Add(item.friends);
            Console.Write("UserID: " + item.friends.userID + ". Activities: ");
            activity = new List<Activity>();
            activity.Add(item.latest_activity);
            Console.Write(item.latest_activity.timestamp);
            foreach (var i in item.next_activities)
            {
                activity.Add(i.Data);
                Console.Write(i.Data.timestamp);
            }
            activities.Add(activity);
            Console.WriteLine();
        }

This is the result of the code above: enter image description here

The order is 3 2 4 (userID) you see.

Could you please explain me why and tell me how to fix ?

Thank you for your help.

1

1 Answers

3
votes

Well, your queries are certainly different; the one using Neo4jclient for example, has a collect in it. They'll follow different execution plans and unless you specify it explicitly, they can return their results in a different order.

Use ORDER BY to specify the order. You can't make any assumptions otherwise.

I've even seen queries return results in a different order when there was only a difference in letter case (lower vs upper case letters).