Neo4jClient ForEach 合并

Neo4jClient ForEach merge

我正在尝试在 neo4jclient 中编写一个查询,以便 add/update 层次结构树的所有部分一次性进入数据库。 不过,在对集合使用合并时我遇到了麻烦。 我看到了 foreach 函数,但没有成功让它工作。

到目前为止,我已经尝试了以下方法 我试过将它写成一个完整的字符串,但一直收到 'Neo4jClient.NeoException: SyntaxException: Invalid input '.' 的异常:需要一个标识符字符、空格、'}' 或 ':'(第 6 行,第 90 列(偏移量:266)) '

如果 foreach 函数像 c# 一样工作但不知道正确的语法,则字符串下方是我想象的代码工作方式。

任何帮助将不胜感激。

    public void Save(CypherAspirationsViewModel aspirations, string emailAddress)
    {
        var query = graphClient.Cypher
            .Match("(user:User)")
            .Where((CypherUser user) => user.EmailAddress == emailAddress)

            // Aspirations
            .Merge("user" + CypherRelationships.Aspirations + ">(aspirations:Aspirations {Guid: {aspirationsGuid}})")
            .OnCreate()
            .Set("aspirations = {aspirations}")
            .WithParams(new
            {
                aspirationsGuid = aspirations.CypherAspirations.Guid,
                aspirations = aspirations.CypherAspirations
            });


        // Permanent Remuneration
        if (aspirations.CypherPermanentRemuneration != null) {
            query = query.Merge("aspirations" + CypherRelationships.PermanentRemuneration + ">(permanentRemuneration:Remuneration {Guid: {permanentRemunerationGuid}})")
                .OnCreate()
                .Set("permanentRemuneration = {permanentRemuneration}")
                .WithParams(new
                {
                    permanentRemunerationGuid = aspirations.CypherPermanentRemuneration.Guid,
                    permanentRemuneration = aspirations.CypherPermanentRemuneration
                });
            }

        // Contract Remuneration
        if (aspirations.CypherContractRemuneration != null) {
            query = query.Merge("aspirations" + CypherRelationships.ContractRemuneration + ">(contractRemuneration:Remuneration {Guid: {contractRemunerationGuid}})")
                .OnCreate()
                .Set("contractRemuneration = {contractRemuneration}")
                .WithParams(new
                {
                    contractRemunerationGuid = aspirations.CypherContractRemuneration.Guid,
                    contractRemuneration = aspirations.CypherContractRemuneration
                });
            }

        // Consultant Remuneration
        if(aspirations.CypherConsultantRemuneration != null)
        {
            query = query.Merge("aspirations" + CypherRelationships.ConsultantRemuneration + ">(consultantRemuneration:Remuneration {Guid: {consultantRemunerationGuid}})")
                .OnCreate()
                .Set("consultantRemuneration = {consultantRemuneration}")
                .WithParams(new
                {
                    consultantRemunerationGuid = aspirations.CypherConsultantRemuneration.Guid,
                    consultantRemuneration = aspirations.CypherConsultantRemuneration
                });
        }

        // Locations
        if (aspirations.CypherLocations != null)
        {
            //string forEachString = "(n in {locations} | merge (aspirations " + CypherRelationships.Location + ">(location:Location {Guid: {n.Guid}})) on create set location.Name = n.Name, location.Longitude = n.Longitude, location.Latitude = n.Latitude)";
            //query = query
            //    .ForEach(forEachString)
            //    .WithParam("locations", aspirations.CypherLocations);

            query = query.ForEach("CypherLocation location in aspirations.CypherLocations")
                .Merge("aspirations" + CypherRelationships.Location + ">(location:Location {Guid: {locationGuid}})")
                .OnCreate()
                .Set("location = {location}")
                .WithParams(new
                {
                    locationGuid = location.Guid,
                    location = location
                });
        }

        query.ExecuteWithoutResults();
    }

密码查询:

MATCH (user:User)
WHERE (user.EmailAddress = "email")
MERGE user-[:ASPIRATIONS]->(aspirations:Aspirations {Guid: "0d700793-4702-41ee-99f1-685472e65e51"})
ON CREATE
SET aspirations = {
  "FullTime": true,
  "PartTime": false,
  "Permanent": false,
  "Contract": false,
  "Consultant": false,
  "WillingToRelocate": false,
  "CommuteEnum": 40,
  "Guid": "0d700793-4702-41ee-99f1-685472e65e51"
}


FOREACH (n in [
  {
    "Name": "Location1",
    "Longitude": 10.0,
    "Latitude": 1.0,
    "Guid": "a9f25fda-9559-4723-80ec-d8711a260adc"
  }
] | 
merge (aspirations -[:LOCATION]->(location:Location {Guid: {n.Guid}})) 
on create set location.Name = n.Name, location.Longitude = n.Longitude, location.Latitude = n.Latitude)

好的,我想我已经搞定了。需要对 Cypher 进行一些更改(取决于服务器版本)

首先,最初的错误来自 ForEach 中的最后一个 Merge - 你有 {Guid: {n.Guid}} 但你不需要额外的 {} - 所以应该是:{Guid: n.Guid}.

一旦你明白了,如果你要反对 3.0 DB,你需要在你的合并方法中添加一些括号,例如:

.Merge("user" + CypherRelationships.Aspirations + ">(aspirations:Aspirations {Guid: {aspirationsGuid}})")

应该变成:

.Merge("user" + CypherRelationships.AspirationsWithClosingParentheses + ">(aspirations:Aspirations {Guid: {aspirationsGuid}})")

其中 AspirationsWithClosingParentheses 类似于:

var AspirationsWithClosingParentheses = "Aspirations)--"

您需要为 每次 合并执行此操作,因为 3.0 需要包围标识符!