我如何在Neo4j图形数据库中检索关系 [英] How do i retrieve a relationship in Neo4j graph database

查看:225
本文介绍了我如何在Neo4j图形数据库中检索关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请忍受我是我的新手: 我目前正在使用.Net neo4jClient.目前,我有一个 Share 节点和一个 Customer 节点.我正在他们之间创建一种关系 CustomerOwnsShare 并将其持久化.

Please bear with me I'm new to this: I'm currently using the .Net neo4jClient. Currently I have a Share node and a Customer node. I'm creating a relationship CustomerOwnsShare between them and persisting it.

这是我的关系课

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

Now to retrieve a list of Relationships back from the Database I'm using Linq as below

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

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

But this returns me RelationshipInstance object which doesn't have the data that i need(Quantity, CostPerShare, etc.).

RelationshipInstance 公开了一个 RelationshipReference 对象,但这甚至无助于我检索实际的 Relationship 对象. 深入研究后,我发现可以执行如下的Raw gremlin查询

RelationshipInstance exposes a RelationshipReference Object, but even that doesn't help me retrieve my actual Relationship Object. On digging a little deeper i see that i can execute Raw gremlin query as below

graphClient.ExecuteGetAllRelationshipsGremlin<>()

,但是该函数的签名也返回了 RelationshipInstance 的IEnumerable.

but the function signature of that also returns me an IEnumerable of RelationshipInstance.

有关如何检索其数据的实际持久关系对象的任何想法或建议?

Any ideas or suggestions on how i can retrieve my actual persisted Relationship object with it's data ??

预先感谢

推荐答案

很抱歉,现在给您发送,您真正想要的是一个'RelationshipInstance<CustomerOwnsShare>'...

Sorry for the time to get this to you, what you actually want is a 'RelationshipInstance<CustomerOwnsShare>'...

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

So, let's pretend I have the following setup:

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

括号中的数字是neo4j参考. 我将使用neo4jclient执行的查询是:

The numbers in brackets are the neo4j references. The query I would perform using neo4jclient is:

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

现在,如果您仅复制/粘贴此内容,则会出现错误:

Now, if you just copy / paste this, you're going to get an error:

'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类中:

Which is a pain, the way around this is to put a parameterless constructor into your CustomerOwnsShare class:

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

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

This is fine for you, as the TargetNode will be set by the deserialiser. You do want to make sure that you don't use that constructor yourself though. The 'EditorBrowsable' will prevent external assemblies from seeing it, but unfortunately won't do anything for any code in the same assembly, so you might want to mark it as:

[Obsolete]

也只是为了提醒自己.

as well, just to act as a reminder to yourself.

这篇关于我如何在Neo4j图形数据库中检索关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆