如何在Neo4j图数据库中检索关系

时间:2012-09-19 08:44:23

标签: neo4j neo4jclient

请耐心等待我对此我是新手: 我目前正在使用.Net neo4jClient。目前,我有共享节点和客户节点。我正在他们之间建立一种关系 CustomerOwnsShare 并坚持下去。

这是我的关系类

public class CustomerOwnsShare :
    Relationship,
    IRelationshipAllowingSourceNode<Customer>,
    IRelationshipAllowingTargetNode<Share>
{
    public CustomerOwnsShare(NodeReference targetNode)
        : base(targetNode)
    {

    }

    public int Quantity { get; set; }
    public float CostPerShare { get; set; }
    public string DateOfPurchase { get; set; }
    public string ShareSymbol { get; set; }

    public const string TypeKey = "CUSTOMER_OWNS_SHARE";
    public override string RelationshipTypeKey
    {
        get { return TypeKey; }
    }
}

现在从数据库中检索一个关系列表,我正在使用Linq,如下所示

IEnumerable<RelationshipInstance> relationshipInstances =
            graphClient.RootNode.In<Customer>(CustomerBelongsTo.TypeKey, c => c.Email == email)
            .OutE(CustomerOwnsShare.TypeKey)

但是这会返回 RelationshipInstance 对象,它没有我需要的数据(Quantity,CostPerShare等)。

RelationshipInstance 会公开 RelationshipReference 对象,但即使这样也无法帮助我检索实际的关系对象。 在深入挖掘时,我发现我可以执行Raw gremlin查询,如下所示

graphClient.ExecuteGetAllRelationshipsGremlin<>()

但是它的函数签名也返回了IEnumerable RelationshipInstance

关于如何使用它的数据检索我的实际持久关系对象的任何想法或建议

先谢谢

1 个答案:

答案 0 :(得分:3)

很抱歉有时间告诉你,你真正想要的是'RelationshipInstance<CustomerOwnsShare>'......

所以,让我假装我有以下设置:

Root(0) -[]-> User(1) -[CUSTOMER_OWNS_SHARE]-> MSFT(2)

括号中的数字是neo4j引用。 我将使用neo4jclient执行的查询是:

var results = graphClient.ExecuteGetAllRelationshipsGremlin<CustomerOwnsShare>("g.v(2).inE", null);
var quant = results[0].Data.Quantity; //etc

现在,如果你只是复制/粘贴它,你将会收到一个错误:

'CustomerOwnsShare' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TData' in the generic type or method 'Neo4jClient.GraphClient.ExecuteGetAllRelationshipsGremlin<TData>(string, System.Collections.Generic.IDictionary<string,object>)'

这是一种痛苦,解决方法是将无参数构造函数放入CustomerOwnsShare类中:

[EditorBrowsable(EditorBrowsableState.Never)]
public CustomerOwnsShare() : base(0) { }

这对你很好,因为TargetNode将由反序列化器设置。 你想要确保你自己不使用那个构造函数。 'EditorBrowsable'将阻止外部程序集看到它,但遗憾的是它不会对同一程序集中的任何代码执行任何操作,因此您可能希望将其标记为:

[Obsolete]

同样,只是为了提醒自己。