【问题标题】:Neo4jClient get collection of types from all relationsNeo4jClient 从所有关系中获取类型的集合
【发布时间】:2021-11-10 11:44:20
【问题描述】:

我正在尝试将在 Neo4j 图形界面中工作的特定查询转换为使用 C# 中的 Neo4jClient 的实现。

我想在一个列表中获取结果节点的所有关系类型。这可以使用以下查询:

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

这会导致

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

在 C# 中使用客户端时,我无法同时使用 relations.type() 和 relations.CollectAs<List<string>>()。

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

所以最后我想要一个这样的对象:

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

有人能给我指出正确的方向吗?

【问题讨论】:

    标签: c# neo4j neo4jclient


    【解决方案1】:

    您或许可以为此使用.With() 语句:

    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>>(),
                    });
    
    

    【讨论】:

    • 感谢您的快速回复!我也试过这个,但它最初没有用。问题是,如果您开始在 neo4j 查询中使用 WITH,您似乎无法在 with 之前获取变量。所以MATCH ()-[relations]-(p) Where p.name = 'example' WITH collect(type(relations)) as types RETURN p, types limit 10 不起作用,但MATCH ()-[relations]-(p) Where p.name = 'example' WITH collect(type(relations)) as types, with p as outNode RETURN outNode, types limit 10 起作用。
    • 或者换句话说,当使用WITH 时,您可以选择要使用的变量,并且应该对每个变量都这样做。没有WITH,每个变量都可用。
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多