投射未知类型的节点 [英] Casting nodes of an unknown type

查看:64
本文介绍了投射未知类型的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Neo4j时,我可以创建带有标签的节点数组,然后在这些节点之间创建关系.这些标签实质上是到我的POCO的映射(Dog标签与C#中的Dog POCO相关),并且这些POCO是从仅包含ID属性的简单基本POCO中实现的.

In using Neo4j I'm able to create an array of nodes with labels and then create relationships between those nodes. The labels are essentially mappings to my POCOs (the Dog label relates to a Dog POCO in C#) and these POCOs implement from a simple base POCO containing only an ID property.

当我知道要检索的节点的类型/标签时,就可以使用该节点进行转换. T>返回语句中的语法.但是,当执行诸如遍历节点之间的路径之类的操作时,我将不知道所遍历的节点的类型.从技术上讲,可以将节点强制转换为我的POCO所实现的基本类型,但我会丢失所有特定于超类的属性.

When I know the type/label of the node to retrieve, I'm able to cast it using the node.As < T > syntax within the return statement. However, when doing things such as traversing a path between nodes, I will not know the type of the node that I am traversing. While it is technically possible to cast the node as the base type that my POCOs implement from, I lose all of the properties that are specific to the the super class.

关于如何开始使用此方法的任何想法?

Any ideas on how to get started with this one?

推荐答案

您可以(取决于您的感受)尝试使用动态,例如,可以将其设置为:

You could (depending on how you feel about it) try using dynamic, for example, you can set it up like so:

var dog = new Dog {Name = "Woofer", Breed = "Afghan Hound"};
var owner = new Person {Name = "Jeff", PhoneNumber = "01234567890"};

//CREATE
gc.Cypher.
    Create("(owner:Person {ownerParams})")
    .WithParam("ownerParams", owner)
    .With("owner")
    .Create("(owner)-[:HAS_PET]->(dog:Dog {dogParams})")
    .WithParam("dogParams", dog)
    .ExecuteWithoutResults();

并使用以下内容进行检索:

and retrieve with:

//RETURN
var query = gc.Cypher
    .Match("(p:Person)-[:HAS_PET]->(d:Dog)")
    .Return((p, d) => new {Person = p.As<Node<string>>(), Dog = d.As<Node<string>>()});

var results = query.Results.ToList();
foreach (var result in results)
{
    dynamic p = JsonConvert.DeserializeObject<dynamic>(result.Person.Data);
    dynamic d = JsonConvert.DeserializeObject<dynamic>(result.Dog.Data);

    Console.WriteLine("If you find {0} (a {1}) please call {2} on {3}.", d.Name, d.Breed, p.Name, p.PhoneNumber);
}

很显然,在这种情况下,我知道我要返回的类型.现在,您会注意到我在其中使用Node<string>-对此通常感到不满意-我使用它的原因是它去除了neo4j返回的所有常规内容,并且分隔Data-这真的是我感兴趣的全部.

Obviously in this case I would know the types I was returning. Now, you'll notice I'm using Node<string> in this - which generally is frowned upon - the reason I'm using it is that it strips out all the normal stuff neo4j returns back, and separates the Data out - which is really all I'm interested in.

您可能会想尝试做:

.Return((p,d) => new {Person = p.As<dynamic>(), Dog = d.As<dynamic>()});

但是您在这里最终遇到的问题是Neo4jClient不处理动态,实际上会将其作为object返回,这会丢失您的所有属性.

but the problem you'll end up with here is that the Neo4jClient doesn't deal with dynamic and will actually return it as an object which loses all your properties.

这至少应该为您提供一个起点,如果您需要有关特定类型查询的帮助,则值得将该查询作为参考.

This should at least give you a starting point, if you need help with a specific type of query it'd be worth putting the query up for reference.

这篇关于投射未知类型的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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