如何从Neo4j - Json输出中删除类元数据

时间:2014-01-24 12:15:57

标签: asp.net json neo4j neo4jclient

我在ASP.Net Web Api项目中有以下代码。我将结果返回给客户。

 var query = graphClient.Cypher
                         .Start(new { user = userNode.Reference, cultureInfo = startNode.Reference })
                         .Match("(cultureInfo)-[:CREATES]->(poll:Poll)-[:HAS]->(option:Option)," +
                                                                                "(poll:Poll)<-[:TAGS]-(hashtag:Hashtag)")
                         .Where("NOT (user)-[:ANSWERED]->(poll:Poll)")
                         .AndWhere("NOT (user)-[:SKIPS]->(poll:Poll)")
                         .OptionalMatch("(theme:Theme)-[:PAINTS]->(poll)")
                         .OptionalMatch("(user1:User)-[:ENJOYS]->(poll)")
                         .OptionalMatch("(user)-[e:ENJOYS]->(poll)")
                         .OptionalMatch("(user2)-[a:ANSWERED]->(poll)")
                         .OptionalMatch("(user)-[s:SEARCHES]->(poll)")
                         .Return((poll, option, hashtag, user1, e, a, theme, s) => new
                                                 {
                                                     Poll = poll.As<Poll>(),
                                                     Options = option.CollectAsDistinct<Option>(),
                                                     Hashtag = hashtag.CollectAsDistinct<Hashtag>(),
                                                     Enjoy = user1.CountDistinct(),
                                                     MyEnjoy = e.CountDistinct(),
                                                     Answers = a.CountDistinct(),
                                                     Theme = theme.As<Theme>(),
                                                     MySearch = s.CountDistinct()
                                                 }).OrderByDescending("poll.CreateDate").Limit(Static.Constant.PageSize);

当我从提琴手测试时,我从我的网络API获取了这样的元数据信息。

[{"poll":{"title":"Güneş gözlüğü","key":"5533637004976467983","createDate":"20131224221100410"},"options":[{"reference":{"id":1287,"nodeType":"MyProject.Domain.Entities.Option, MyProject.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"},"data":{"title":"Kullanmam","imageUrl":null,"viewOrder":2,"key":"5571854810605547089"}},{"reference":{"id":1286,"nodeType":"MyProject.Domain.Entities.Option, MyProject.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"},"data":{"title":"Kullanırım","imageUrl":null,"viewOrder":1,"key":"4676100885647976335"}}],"hashtag":[{"reference":{"id":739,"nodeType":"MyProject.Domain.Entities.Hashtag, MyProject.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"},"data":{"key":"5712268173611849394","name":"HayatTarzı"}}],"enjoy":2,"myEnjoy":1,"answers":2,"theme":{"name":"Sun Flower","backcolor":"#f1c40f","forecolor":"#ffffff","key":"4847432544599442216"},"mySearch":0}]

我的意思是在“options”和“hashtags”对象中,有一些类元数据,如

  

{ “参考”:{ “ID”:1287, “节点类型”:“MyProject.Domain.Entities.Option,   MyProject.Domain,Version = 1.0.0.0,Culture = neutral,   公钥=空“}

我的问题是,如何在发送给客户端之前删除此类数据?

1 个答案:

答案 0 :(得分:1)

CollectAsCollectAsDistinct返回IEnumerable<Node<T>>而不是IEnumerable<T>。它有点尴尬,不太理想,但它是目前的工作方式。

Node<T>有两个属性:ReferenceData。这些就是你所看到的。

您可以将这些数据投射出去:

var query = graphClient.Cypher
    .Start(new { user = userNode.Reference, cultureInfo = startNode.Reference })
    .Match(
        "(cultureInfo)-[:CREATES]->(poll:Poll)-[:HAS]->(option:Option)",
        "(poll:Poll)<-[:TAGS]-(hashtag:Hashtag)"
    )
    .Where("NOT (user)-[:ANSWERED]->(poll:Poll)")
    .AndWhere("NOT (user)-[:SKIPS]->(poll:Poll)")
    .OptionalMatch("(theme:Theme)-[:PAINTS]->(poll)")
    .OptionalMatch("(user1:User)-[:ENJOYS]->(poll)")
    .OptionalMatch("(user)-[e:ENJOYS]->(poll)")
    .OptionalMatch("(user2)-[a:ANSWERED]->(poll)")
    .OptionalMatch("(user)-[s:SEARCHES]->(poll)")
    .Return((poll, option, hashtag, user1, e, a, theme, s) => new
    {
        Poll = poll.As<Poll>(),
        Options = option.CollectAsDistinct<Option>(),
        Hashtag = hashtag.CollectAsDistinct<Hashtag>(),
        Enjoy = user1.CountDistinct(),
        MyEnjoy = e.CountDistinct(),
        Answers = a.CountDistinct(),
        Theme = theme.As<Theme>(),
        MySearch = s.CountDistinct()
    })
    .OrderByDescending("poll.CreateDate")
    .Limit(Static.Constant.PageSize)
    .Results
    .Select(r => new {
        r.Poll,
        Options = r.Options.Select(n => n.Data),
        Hashtag = r.Hashtag.Select(n => n.Data),
        r.Enjoy,
        r.MyEnjoy,
        r.Answers,
        r.Theme,
        r.MySearch
    });

您必须在Results调用后进行投影,以使其不在Return子句中。

pr/12降落时,应该改进这一点,这样你就不需要这样做了。

旁注:您应该努力弃用Start子句。 Node<T>NodeReference在Neo4j 2.0世界中没有多大意义,并且会从Neo4jClient中慢慢弃用。