返回Neo4J后反序列化对象

时间:2017-11-20 17:20:31

标签: neo4j cypher neo4jclient

我想根据参数计算两个节点之间的最短路径。

所以,我有这个问题。

MATCH p = (po:Ponto)-[r:VAI_PARA*]->(pd:Ponto)
WHERE 1 = 1
AND (po.PontoId = {p0})
AND (pd.PontoId = {p1})
WITH with p, reduce(d = 0, r in relationships(p) | d + r.Distancia) as totalDist
RETURN p AS path, totalDist AS Distance
ORDER BY totalDist DESC
LIMIT {p4}

在C#中,我写了这个查询:

var cypherQuery = _uow.GraphClient.Cypher
    .Match($"p = (po:{PontoMapa.Label})-[r:{CaminhoRelacao.Label}*]->(pd:{PontoMapa.Label})")
    .Where("1 = 1")
    .AndWhere((PontoMapa po) => po.PontoId == query.PontoOrigemId)
    .AndWhere((PontoMapa pd) => pd.PontoId == query.PontoDestinoId)
    .With("p, reduce(d = 0, r in relationships(p) | d + r.Distancia) as totalDist")
    .WithParams(new
    {
        query.PontoOrigemId,
        query.PontoDestinoId
    })
    .Return((p, totalDist) => new
    {
        path = p.As<dynamic>(),
        Distance = totalDist.As<int>()
    })
    .OrderByDescending("totalDist")
    .Limit(1);

它正在返回这样的东西:

╒══════════════════════════════╤═══════════╕
│"p"                           │"totalDist"│
╞══════════════════════════════╪═══════════╡
│[{"PontoDeEntrada":true,"Ponto│40         │
│Id":"0eac5620-aeca-4b26-a3f2-5│           │
│631825829cb","Identificador":"│           │
│CTUPHA-0001"},{"Distancia":10}│           │
│,{"PontoDeEntrada":false,"Pont│           │
│oId":"40e00556-8625-4914-9774-│           │
│3f1a18962880","Identificador":│           │
│"CTUPHA-0002"},{"PontoDeEntrad│           │
│a":false,"PontoId":"40e00556-8│           │
│625-4914-9774-3f1a18962880","I│           │
│dentificador":"CTUPHA-0002"},{│           │
│"Distancia":10},{"PontoDeEntra│           │
│da":false,"PontoId":"ed856f2b-│           │
│f28f-4e5b-b69d-d4617247488e","│           │
│Identificador":"CTUPHA-0003"},│           │
│{"PontoDeEntrada":false,"Ponto│           │
│Id":"ed856f2b-f28f-4e5b-b69d-d│           │
│4617247488e","Identificador":"│           │
│CTUPHA-0003"},{"Distancia":10}│           │
│,{"PontoDeEntrada":false,"Pont│           │
│oId":"a7f8028f-9bd1-485e-899b-│           │
│0902e159a9fd","Identificador":│           │
│"CTUPHA-0004"},{"PontoDeEntrad│           │
│a":false,"PontoId":"a7f8028f-9│           │
│bd1-485e-899b-0902e159a9fd","I│           │
│dentificador":"CTUPHA-0004"},{│           │
│"Distancia":10},{"PontoDeEntra│           │
│da":false,"PontoId":"1a9b9e3a-│           │
│0333-417f-a5c5-e191dee8b9db","│           │
│Identificador":"CTUPHA-0005"}]│           │
└──────────────────────────────┴───────────┘

但是当我尝试反序列化返回时,路径为空。

如何创建适合此数据的模型?

或者,是否存在任何可以帮助我解决此问题的查询,以另一种方式编写?

1 个答案:

答案 0 :(得分:0)

您无法使用dynamic转换为,因此p.As<dynamic>()将始终失败并为您提供空值。您需要撤回的是PathsResult

var cypherQuery = _uow.GraphClient.Cypher
    .Match($"p = (po:{PontoMapa.Label})-[r:{CaminhoRelacao.Label}*]->(pd:{PontoMapa.Label})")
    .Where("1 = 1")
    .AndWhere((PontoMapa po) => po.PontoId == query.PontoOrigemId)
    .AndWhere((PontoMapa pd) => pd.PontoId == query.PontoDestinoId)
    .With("p, reduce(d = 0, r in relationships(p) | d + r.Distancia) as totalDist")
    .WithParams(new
    {
        query.PontoOrigemId,
        query.PontoDestinoId
    })
    .Return((p, totalDist) => new
    {
        path = p.As<PathsResult>(), //<< Change here
        Distance = totalDist.As<int>()
    })
    .OrderByDescending("totalDist")
    .Limit(1);

你可能想从p考虑你想要的东西 - 你想要节点吗?在这种情况下,使用nodes(p)进行游戏 - PathsResult可能不是您实际需要的内容:/