我有一个C#对象模型,我希望与Neo4j保持一致。它包含数十个项目,每个项目有1000-4000个孩子。我目前使用官方的C#驱动程序,但我可以切换到别的东西。毫无疑问,一次这样做一个孩子效率低下。一个项目需要几分钟时间:
foreach (var service in project.Services)
{
using (var session = _driver.Session())
{
var query = "MATCH (p:Project) WHERE ID(p) = " + neoId +
" CREATE (ds:Service {name:\"" + service.Name +
"\"})<-[:HAS_SERVICE]-(p)";
var result = session.Run(query);
}
}
我是Cypher noob,但我认为我应该将孩子作为参数传递给涉及UNWIND
或FOREACH
的查询,但我不清楚这将如何以编程方式发生Run()
的可选第二个参数是Dictionary
或object
(不清楚该对象将如何解释)。
那么,如何从C#中有效地做到这一点?
答案 0 :(得分:2)
1)首先,您需要创建一个包含服务名称数组的字典。
2)内部查询使用UNWIND扩展它们。
var servicesNames_ = new List<string>();
foreach (var service in project.Services)
{
servicesNames_.Add( service.Name );
}
var servicesName = servicesNames_.ToArray();
var params = new Dictionary<string, object> {
{"services", servicesName},
{"neoId", neoId}
};
using (var session = _driver.Session())
{
var query = "WITH {neoId} as neoId, {services} as services " +
"MATCH (p:Project) WHERE ID(p) = {neoId} " +
"UNWIND services as serviceName " +
"CREATE (ds:Service {name: serviceName})<-[:HAS_SERVICE]-(p) ";
var result = session.Run(query, params);
}