Itay Tur
Itay Tur

Reputation: 1679

Neo4jClient - Expression of type System.Linq.Expressions.ConstantExpression is not supported

Can't create an object and set it's Boolean property inside the neo4jclient query.

I'm doing a project with neo4jclient so far it worked fine. But now when i try setting a property of an object created in a query to true it throws this exception:

Expression of type System.Linq.Expressions.ConstantExpression is not supported.

the query is simple:

            return await _graphClient.Cypher.Match("(u1:User)-[:FOLLOW]->(u2:User)")
                .Where((UserModel u1) => u1.Id == userId)
                .Return(u2 => new UserWithRelationsDto { User=u2.As<UserModel>(), IsFollow = true })
                .Limit(usersToShow)
                .ResultsAsync;

I tried using Boolean varible with true value. but than it throws a different exception:

The expression value(DAL.Repositories.Neo4jUsersRepository+<>c__DisplayClass5_1).isFollow is not supported

var isFollow = true;
            return await _graphClient.Cypher.Match("(u1:User)-[:FOLLOW]->(u2:User)")
                .Where((UserModel u1) => u1.Id == userId)
                .Return(u2 => new UserWithRelationsDto { User=u2.As<UserModel>(), IsFollow = isFollow })
                .Limit(usersToShow)
                .ResultsAsync;

the query works if i take the bool property out. probably bug? and is there a work around?

Upvotes: 0

Views: 297

Answers (2)

Charlotte Skardon
Charlotte Skardon

Reputation: 6270

Sorry I missed this, there is another way and that's to use the With statement:

    var isFollow = false;
    await _graphClient.Cypher
        .Match("(u:User)")
        .With($"{{IsFollow:{isFollow}, User:u}} AS u2")
        .Return(u2 => u2.As<UserWithRelationsDto>())
        .ResultsAsync;

Or you could just use true in place:

    await _graphClient.Cypher
        .Match("(u:User)")
        .With($"{{IsFollow:{true}, User:u}} AS u2")
        .Return(u2 => u2.As<UserWithRelationsDto>())
        .ResultsAsync;

Or perhaps simplest is just:

    await _graphClient.Cypher
        .Match("(u:User)")
        .With("{IsFollow:true, User:u} AS u2")
        .Return(u2 => u2.As<UserWithRelationsDto>())
        .ResultsAsync;

Either way - .With allows you to create an anonymous type in Cypher that you can parse directly into your DTO.

Upvotes: 1

Itay Tur
Itay Tur

Reputation: 1679

The work around:

return await _graphClient.Cypher.Match("(u1:User)-[r:FOLLOW]->(u2:User)")
                .Where((UserModel u1) => u1.Id == userId)
                .Return((u2,r) => new UserWithRelationsDto { User=u2.As<UserModel>(), IsFollow = r!=null})
                .Limit(usersToShow)
                .ResultsAsync;

Upvotes: 0

Related Questions