1
votes

I am developing a social photo app in .NET using Neo4jClient to talk to a Neo4j graph database. I want to get all photos which a specific user hasn't already seen which I can accomplished with the cypher query:

MATCH (user:User)-[:USER_PHOTO]-(photo:Photo) 
OPTIONAL MATCH (photo)-[r:USER_SEEN_PHOTO]-(currentUser:User) 
WHERE (currentUser.Id = 'user2') 
WITH photo,user, count(currentUser) AS cnt 
WHERE cnt = 0 
RETURN DISTINCT photo, user;

Unfortunalety I don't know how to correctly translate this to Neo4jClient. I tried below query but it doesn't work as expected and it's returning photos user2 already seen:

var graphResults = await graphClient.Cypher
            .Match("(user:User)-[:USER_PHOTO]->(photo:Photo)")
            .OptionalMatch("user-[:USER_SEEN_PHOTO]-(currentUser:User)")
            .Where((UserEntity currentUser) => currentUser.Id == currentUserId)
            .With("user, photo, count(currentUser) AS cnt")
            .Where("cnt = 0")
            .ReturnDistinct((photo, user) => new
            {
                Photo = photo.As<PhotoEntity>(),
                User = user.As<UserEntity>()
            }).ResultsAsync;
1

1 Answers

1
votes

If you have a look at your .OptionalMatch statement, I think you need to change it from:

.OptionalMatch("user-[:USER_SEEN_PHOTO]-(currentUser:User)")

to

.OptionalMatch("photo-[:USER_SEEN_PHOTO]-(currentUser:User)")
                 ^^^