Search code examples
c#neo4jneo4jclient

Neo4jClient get collection of types from all relations


I'm trying to convert a specific query, which works in the Neo4j graphical interface, to an implementation using the Neo4jClient in C#.

I want to get all the relation types of the result nodes in a list. This is possible using the following query:

MATCH ()-[relations]-(p)
Where p.name = 'example'
RETURN count(relations) AS relCount, collect(type(relations)) as types limit 10

This results in

relCount types
4 ["died_in", "has_type", "was_born", "is_identified_by"]
2 ["has_type", "is_identified_by"]

I can't wrap my head around using both relations.type() in combination with relations.CollectAs<List<string>>() when using the client in C#.

var q = client.Cypher
                .Match(@"()-[relations]-(p)")
                .Where("p.name = 'example'")
                .Return((relations, p) => new
                {
                    relCount = relations.Count(),
                    types = relations.CollectAs<List<string>>(),
                });

So in the end I want to have an object like this:

[
  {
    relcount: 4,
    types: ["died_in", "has_type", "was_born", "is_identified_by"]
  },
  {
    relcount: 2,
    types: ["has_type", "is_identified_by"]
  }
]

Could somebody point me in the right direction?


Solution

  • You can probably use a .With() statement for this:

    var q = client.Cypher
                    .Match(@"()-[relations]-(p)")
                    .Where("p.name = 'example'")
                    .With("count(relations) AS relCount, collect(type(relations)) as types")
                    .Return((relCount, types) => new
                    {
                        relCount = relCount.As<int>(),
                        types = types.As<List<string>>(),
                    });