在查询neo4j客户端上排除属性

时间:2016-06-03 17:26:15

标签: neo4jclient

我正在制作一个类似于小型网络的应用程序。 我有这门课:

class Person
{
    public string Email {get; set;}
    public string Password {get; set;}
    public int Age {get; set;}
    public long Birthday {get;set;}
    ... 30 properties more
}

我有这个问题:

_graphClient.Cypher.Match ("n:Person")
    .Where<Person>(n => n.Email == info.Email)
    .Return(n => n.As<Person>)

但我希望我的查询忽略密码并返回所有其他属性。

确切地说,我希望它是:

{
 Email: "email_1",
 Age : 10,
 Birthday : 1000
 ... 30 properties more
}

有人可以帮我吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

我可以想到三种选择,但我认为只有一种选择对你来说真的可行。您可以将属性[JsonIgnore]附加到Password属性 - 但这将阻止它被序列化 - 而且我认为您不希望这样。

第二种选择是使用Anonymous类型进行返回,例如:

.Return(n => new {
        Email = n.As<Person>().Email,
        /* etc */
    })

但我认为这意味着你会编写很多代码,我想你可以避免这些代码。

最后一个选项是:

public class Person {
    public string Email { get; set; }
    /* All other properties, except Password */
}

public class PersonWithPassword : Person {
    public string Password { get;set; }
}

然后使用Person您所在的位置,以及PersonWithPassword您需要密码的地方。

如果您将会员资格设置为按原样使用Person,您可能会发现自己遇到问题 - 在这种情况下,您可以执行以下操作:

public class PersonWithoutPassword {
    public string Email { get; set; }
    /* All other properties, except Password */
}

public class Person : PersonWithoutPassword {
    public string Password { get;set; }
}

并且在您回来时 - 您将返回:

Return(n => n.As<PersonWithoutPassword>())

代替。