Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Neo4jClient中动态使用关系参数_C#_Neo4j_Neo4jclient - Fatal编程技术网

C# 在Neo4jClient中动态使用关系参数

C# 在Neo4jClient中动态使用关系参数,c#,neo4j,neo4jclient,C#,Neo4j,Neo4jclient,我试图使用params与Neo4jclient动态传递关系类型,但似乎不起作用。这可能吗?或者,等价物是什么?基本上,我试图编写一个实用方法,通过简单地传递两个节点Id和一个关系,将两个节点关联在一起。我可能会对其进行硬编码,但我担心这违反了最佳实践,并且容易受到注射的影响。谢谢 public static async Task<string> AddEdge(string node1Id, string relatioinship, string node2Id, bool

我试图使用params与Neo4jclient动态传递关系类型,但似乎不起作用。这可能吗?或者,等价物是什么?基本上,我试图编写一个实用方法,通过简单地传递两个节点Id和一个关系,将两个节点关联在一起。我可能会对其进行硬编码,但我担心这违反了最佳实践,并且容易受到注射的影响。谢谢

    public static async Task<string> AddEdge(string node1Id, string relatioinship, string node2Id, bool directed = false)
    {

            await NeoClient.Cypher
                    .Match("(n1)", "(n2)")
                    .Where((BaseObject n1) => n1.Id == node1Id)
                    .AndWhere((BaseObject n2) => n2.Id == node2Id)
                    .CreateUnique("n1-[:{sRelationName}]->n2")
                    .WithParams(new {sRelationName = relatioinship})
                    .ExecuteWithoutResultsAsync();
            return node1Id;
    }
public static async Task AddEdge(字符串node1Id、字符串relatioinship、字符串node2Id、bool directed=false)
{
等待新客户,塞弗
.Match(“(n1)”,“(n2)”)
.Where((BaseObject n1)=>n1.Id==node1Id)
.AndWhere((基本对象n2)=>n2.Id==node2Id)
.CreateUnique(“n1-[:{sRelationName}]->n2”)
.WithParams(新的{sRelationName=relatioinship})
.ExecuteWithoutResultsAsync();
返回node1Id;
}

我认为您无法通过参数创建关系名称,我在
C#
中见过的唯一方法是对
.CreateUnique
使用
string.Format

如果您担心注入,一种解决方案可能是使用
Enum
,因此:

public enum Relationships { Rel_One, Rel_Two }

public static async Task<string> AddEdge(string nodeId1, Relationships relationship, string nodeId2)
{
    if(!Enum.IsDefined(typeof(Relationships), relationship))
        throw new ArgumentOutOfRangeException("relationship", relationship, "Relationship is not defined.");
如果您在
关系
枚举中根据需要命名值:

public enum Relationships {
    HAS_A,
    IS_A
    //etc
}

另一个好处是您不必担心拼写错误,因为您可以在整个代码中使用
关系
枚举进行查询。

谢谢Chris。我更仔细地查看了neo4j文档,我认为它不能通过Cypher查询来完成。我确实在代码中使用了enum,但不是我所做的快速测试。谢谢你的帮助!
public enum Relationships {
    HAS_A,
    IS_A
    //etc
}