通过 ids 链接两种类型的所有节点

Linking All Nodes of two types by ids

使用以下代码我创建了两种类型的节点。在 Neo4J

  public async Task CreateData()
  {
     AddNodesAsync<Companies>(myCompanies);
     AddNodesAsync<Employess>(myEmployes);
  }

  public Task AddNodesAsync<T>(List<T> nodes)
    {
        return client
            .Cypher
            .Create("(n:" + typeof(T).Name + " {nodes})")
            .WithParams(new {nodes})
            .ExecuteWithoutResultsAsync();
    }

每个员工都有一个公司编号。 现在我想在员工的公司 ID 与公司 ID 匹配时关联所有这些。

使用下面的代码我可以link一个雇用一个公司

await client.Cypher
            .Match("(company:Company)", "(employee:Employee)")
            .Where((employee: company) => employee.ComanyId == company.Id)                       
            .Create("employee-[:BELONGS_TO]->company")
            .ExecuteWithoutResultsAsync();

但是我怎么知道它应该 link all employed by this id ref to their correspondand company?

我假设某处有错别字,这是我使用的代码,它按您所说的那样生成链接。

将这些用作 POCO:

public class Company {
    public int Id { get; set; }
}

public class Employee {
    public int Id { get; set;}
    public int CompanyId { get; set;}
}

添加它们:

var companies = new List<Company> { new Company { Id = 1 }, new Company {  Id = 2 } };
var employees = new List<Employee>
{
    new Employee{ Id=1, CompanyId =1 },
    new Employee{ Id=2, CompanyId =1 },
    new Employee{ Id=3, CompanyId =1 },
    new Employee{ Id=4, CompanyId =2 },
    new Employee{ Id=5, CompanyId =2 },
};

await AddNodesAsync(companies);
await AddNodesAsync(employees);

下一位会按您的预期生成图表,所有关系都已正确创建。

await client.Cypher
    .Match("(company:Company)","(employee:Employee)")
    .Where((Company company, Employee employee) => employee.CompanyId == company.Id)
    .Create("(employee)-[:BELONGS_TO]->(company)")
    .ExecuteWithoutResults();