2014-01-07 104 views
1

我使用Neo4j數據庫處理C#.net項目,無法在使用C#.Net的Neo4j中的現有節點和新節點之間創建關係。如何使用Neo4jClient在C#.Net中創建neo4j現有節點和新節點之間的關係?

我使用下面的代碼

// Create entities 
var refA = client.Create(new Person() { FName = "Person A" }); 
var refB = client.Create(new Person() { FName = "Person B" }); 
var refC = client.Create(new Person() { FName = "Person C" }); 
var refD = client.Create(new Person() { FName = "Person D" }); 

// Create relationships 
client.CreateRelationship(refA, new KnowsRelationship(refB)); 
client.CreateRelationship(refB, new KnowsRelationship(refC)); 
client.CreateRelationship(refB, new HatesRelationship(refD, new HatesData("Crazy guy"))); 
client.CreateRelationship(refC, new HatesRelationship(refD, new HatesData("Don't know why..."))); 

現在我想創建現有節點使用一些條件,創造新的節點,從數據庫中獲取的一個關係創建節點。但不能這樣做

+0

請提供更多詳細信息 –

+1

*「但不能做到這一點」* - 請編輯您的問題,以顯示您已經嘗試過。 –

+0

請寫出更多關於你想要完成的細節的細節,因爲它確實很難提供幫助。謝謝! –

回答

1

如果你不想使用C#中的Neo4J,你仍然需要對Cypher有一個很好的理解。

你可以在這裏找到它很文檔:

Cypher query language - documentation

我已經創建了一個示例程序爲您創造3個節點以及它們之間的關係2,然後查詢節點neigbouring節點1

using Neo4jClient; 
using Neo4jClient.Cypher; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Neo4JTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data")); 
       client.Connect(); 

       // Create nodes and relationship 
       MyNode node1 = new MyNode() { Name = "Test 1" }; 
       MyNode node2 = new MyNode() { Name = "Test 2" }; 

       NodeReference<MyNode> node1ref = client.Create<MyNode>(node1); 
       NodeReference<MyNode> node2ref = client.Create<MyNode>(node2); 

       MyRelationShip rel12 = new MyRelationShip(node2ref); 

       var Rel1 = client.CreateRelationship<MyNode, MyRelationShip>(node1ref, rel12);    

       MyNode node3 = new MyNode() { Name = "Test 3" }; 
       NodeReference<MyNode> node3ref = client.Create<MyNode>(node3); 

       MyRelationShip rel13 = new MyRelationShip(node3ref); 
       var Rel13 = client.CreateRelationship<MyNode, MyRelationShip>(node1ref, rel13); 

       var query = client.Cypher.Start(new { n1 = node1ref }) 
             .Match("n1-[:MYRELATIONSHIP]->targetnode") 
             .Return<MyNode>(targetnode => targetnode.As<MyNode>()); 
       var res = query.Results; 

       int i = 0; 
       foreach (MyNode n in res) 
       { 
        i++; 
        Console.WriteLine(i + ". Name: '" + n.Name + "'"); 
       } 
      } 
      catch(NeoException ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
      Console.ReadKey(); 
     } 


     class MyNode 
     { 
      private string _name = ""; 

      public string Name 
      { 
       get 
       { 
        return _name; 
       } 

       set 
       { 
        _name = value; 
       } 
      } 
     } 
     public class MyRelationShip : Relationship, IRelationshipAllowingSourceNode<MyNode>, IRelationshipAllowingTargetNode<MyNode> 
     { 
      public static readonly string TypeKey = "MYRELATIONSHIP"; 

      public MyRelationShip(NodeReference targetNode) 
       : base(targetNode) 
      { } 

      public override string RelationshipTypeKey 
      { 
       get { return TypeKey; } 
      } 
     } 
    } 
} 
相關問題