我得到了2万个苹果。
如何以比这更智能的方式创建它们?
foreach (var a in apples)
{
graphClient.Cypher
.Create("(a:Apple {newApple})")
.WithParam("newApple", a)
.ExecuteWithoutResults();
}
寻找一种通用方法,以便在不指定每个属性的情况下传递对象。
class Fruit
{
[JsonProperty(PropertyName = "Color")]
public bool Color { get; set; }
}
class Apple : Fruit
{
[JsonProperty(PropertyName = "Variety")]
public String Variety { get; set; }
}
我想apples
是一个字典列表。在这种情况下,纯 Cypher 中更优化的查询将如下所示:
UNWIND {apples} AS newApple
CREATE (a:Apple)
SET a = newApple
我还没有使用过Neo4jClient .NET库,但是沿着这些行应该可以工作:
graphClient.Cypher
.Unwind(apples, "newApple")
.Create("(a:Apple)")
.Set(...)
.ExecuteWithoutResults();
20k 节点可能在单个事务中工作,但值得实现一些批处理并使用大约 10k 个节点的批处理。
更新。根据克里斯·斯卡登的建议更新了实现。
备注。在 Cypher 查询中,如果您使用的是 Neo4j 3.2+,则应切换到新的参数语法,该语法使用 $param
样式参数,因此查询稍微更容易阅读:
UNWIND $apples AS newApple
CREATE (a:Apple)
SET a = newApple