Neo4jClient显示传递对象的奇怪行为

时间:2017-02-15 16:54:29

标签: neo4jclient

我试图使用ASP.NET MVC5中的Neo4jClient从我的Neo4j数据库中查询数据和对象。

我必须在特定对象中构造返回的数据。这适用于IEnumerates,但不适用于单个对象。我无法看到这种行为的任何原因,因为Neo4j本身正以预期的方式返回结果。

这是一个示例:

public class ProjectContainer
{
    public string relationship { get; set; }
    public int accesstype { get; set; }
    public ProjectData data { get; set; }
}

var query = graphClient.Cypher
    .Match("(positionData:Position)<-[relationData:HAS_POSITION]-(projectData:Project)")
    .Where((PositionData positionData) => positionData.uuid == uuid)
    .With("positionData, projectData, { relationship: type(relationData), accesstype:0, data: projectData } as container ")
    .Return((positionData, projectData, container) => new 
    {
        position = positionData.As<PositionData>(),
        projectdata = projectData.As<ProjectData>(),
        projectcontainer = container.As<ProjectContainer>(),
        projects = container.CollectAs<ProjectContainer>()
    });

var pos = query.Results.First();

Neo4j为容器返回单个JSON对象。 pos.projects包含 - 正如预期的那样 - 包含ProjectContainer的1个元素的枚举,包括项目数据的所有属性&#34; data&#34;。但我不想要枚举,因为它总是一个项目。

问题是:

  1. 问题:pos.projectscontainer不为null,但只包含null-properties。它应该由与项目[0]相同的内容填充,但这不起作用。

  2. 问题:如果我重命名该属性&#34;数据&#34; in&#34; project&#34; (属性本身和with子句),甚至pos.projectcontainer也将为null。为什么属性的名称会改变行为,是&#34;数据&#34;客户的任何特殊关键字?

  3. 我在Neo4j浏览器中测试了来自query.Query.DebugQueryText的查询,结果是预期的,即使我将该属性命名为&#34; project&#34;或&#34;数据&#34;:

    MATCH (positionData:Position)<-[relationData:HAS_POSITION]-(projectData:Project) WHERE (positionData.uuid = "c3a1eedc-5083-4784-a378-cfd2ba0bec57") WITH positionData, projectData, { relationship: type(relationData), accesstype:0, data: projectData } as container  RETURN positionData AS position, projectData AS projectdata, container AS projectcontainer, collect(container) AS projects
    
    ╒══════════════════════════════╤══════════════════════════════╤══════════════════════════════╕
    │"projectdata"                 │"projectcontainer"            │"projects"                    │
    ╞══════════════════════════════╪══════════════════════════════╪══════════════════════════════╡
    │{"description":"Project dummy │{"relationship":"HAS_POSITION"│[{"relationship":"HAS_POSITION│
    │description","closed":false,"a│,"accesstype":"0","data":{"des│","accesstype":"0","data":{"de│
    │ctive":true,"published":false,│cription":"Project dummy descr│scription":"Project dummy desc│
    │"title":"Project dummy title",│iption","closed":false,"active│ription","closed":false,"activ│
    │"uuid":"e4327251-d0c7-4e24-aa1│":true,"published":false,"titl│e":true,"published":false,"tit│
    │2-cf7ade4a512a"}              │e":"Project dummy title","uuid│le":"Project dummy title","uui│
    │                              │":"e4327251-d0c7-4e24-aa12-cf7│d":"e4327251-d0c7-4e24-aa12-cf│
    │                              │ade4a512a"}}                  │7ade4a512a"}}]                │
    └──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
    

    感谢您的帮助。 莱纳

1 个答案:

答案 0 :(得分:0)

你是对的,这是一个现有的问题 - 所以这本身并不是一个解决方案,而是一个解决方案来解决这个问题。

我不知道如何用anonymous types来实现你想要的东西,只有一个具体的类类型,所以如果你有一个名为Altogether的类,它看起来像这样: / p>

public class Altogether
{
    public PositionData position { get; set; }
    public ProjectData projectdata { get; set; }
    public ProjectContainer projectcontainer { get; set;}
}

然后,您可以将此行添加到当前查询中:

.With("{position: positionData, projectdata: projectData, projectcontainer: container} as altogether")

然后返回,所以整个查询看起来像:

var query = graphClient.Cypher
    .Match("(positionData:Position)<-[relationData:HAS_POSITION]-(projectData:Project)")
    .Where((PositionData positionData) => positionData.uuid == uuid)
    .With("positionData, projectData, { relationship: type(relationData), accesstype:0, data: projectData } as container")
    .With("{position: positionData, projectdata: projectData, projectcontainer: container} as altogether") //<-- Add here
    .Return((altogether) => altogether.As<Altogether>()); //<-- Change here

它不是很好,但它会正确填充你的对象:/