如何从 Neo4J .NET 客户端返回整个节点及其所有属性? [英] How to return the whole node with all its properties from Neo4J .NET client?

查看:85
本文介绍了如何从 Neo4J .NET 客户端返回整个节点及其所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个通用代码,可以过滤连接到用户的对象.这些对象可以是不同的类型,具有不同的属性等.

I'm trying to make a generic code that can filter on object connected to an user. These objects can be of different types, with different properties etc.

基本上我想实现这个方法:

Basically I want to implement this method:

public string GetRelatedObjects(string sourceObject, string userId){
  var resQuery = GraphDB.Cypher
    .Match("(src:" + sourceObject + ")--(usr:User { Id:{userId} })")
    .WithParam("userId", userId)
    .Return(src => src.As<object>());
  var result = await resQuery.ResultsAsync;

  return JsonConvert.SerializeObject(result);
}

问题是当我使用 .As() 时,我得到一个空项目.

The issue is when I use .As<object>() I get back an empty item.

当我输入一个具体类型时,例如 .As<User>() 我会得到我期望的结果.有没有办法通过 Neo4JClient 获得我想要的东西,或者我必须以某种方式降低级别?

When I put a concrete type, such as .As<User>() I get back the results I expect. Is there a way to get what I'm trying to get with Neo4JClient or do I have to go lower level somehow?

推荐答案

因此,Neo4jClient 在底层使用 Json.NET 将 Neo4j 的输出反序列化为您指定的类.当您指定没有属性的 object 时,您不会获得任何属性.Neo4jClient 和 Json.NET 都不知道你真正想要的是什么,所以你只能取回 object .现在,解决方案时间 - 您可以使用 dynamic 代替.

So under the hood Neo4jClient uses Json.NET to deserialize the output from Neo4j into the class you specify. As you specify object which has no properties you get no properties. Neither Neo4jClient nor Json.NET know what your actually after, so you can only get the object back. Now, solution time - you can use dynamic instead.

public async Task<string> GetRelatedObjectsAsJson(string sourceObject, string userId)
{
    var resQuery = GraphDB.Cypher
        .Match(string.Format("(src:{0})--(usr:User {{ Id:{{userId}} }})", sourceObject))
        .WithParam("userId", userId)
        .Return(src => src.As<Node<string>>());
    var result = await resQuery.ResultsAsync;

    var output = result.Select(node => JsonConvert.DeserializeObject<dynamic>(node.Data)).ToList();
    return JsonConvert.SerializeObject(output);
}

我认为这会得到你想要的,如果 是你,我可能会返回一个 Task> 而不是重新序列化为 string,但我不知道你想将结果用于什么,所以可能不合适.

I think this will get what you want, if I were you, I would probably return an Task<IEnumerable<dynamic>> instead of reserializing to a string, but I don't know what you want to use the results for, so maybe not appropriate.

有关详细信息,请参阅投射未知类型的节点.

Please see Casting nodes of an unknown type for more info.

这篇关于如何从 Neo4J .NET 客户端返回整个节点及其所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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