2013-05-08 107 views
1

想看看在查詢這樣的關係:Neo4jClient - 查詢關係

var query = _graph.Cypher.Start(
new 
{ 
    me = Node.ByIndexLookup("node_auto_index", "id", p.id) 
}).Match("me-[r:FRIENDS_WITH]-friend") 
.Where((Person friend) => friend.id == f.id) 
.Return<FriendsWith>("r"); 

這裏是FriendsWith類。我無法爲FriendsWith添加無參數構造函數,因爲基類(關係)沒有無參數構造函數。

public class FriendsWith : Relationship, 
     IRelationshipAllowingSourceNode<Person>, 
     IRelationshipAllowingTargetNode<Person> 
    { 
     public FriendsWith(NodeReference<Person> targetNode) 
      : base(targetNode) 
     { 
      __created = DateTime.Now.ToString("o"); 
     } 
     public const string TypeKey = "FRIENDS_WITH"; 
     public string __created { get; set; } 
     public override string RelationshipTypeKey 
     { 
      get { return TypeKey; } 
     } 

    } 

但我得到錯誤「沒有無參數的構造函數爲這個對象定義。」當我嘗試運行它。什麼是返回查詢關係的正確方法?

堆棧跟蹤

在Neo4jClient.Deserializer.CypherJsonDeserializer 1.Deserialize(String content) in c:\TeamCity\buildAgent\work\f1c4cf3efbf1b05e\Neo4jClient\Deserializer\CypherJsonDeserializer.cs:line 53 at Neo4jClient.GraphClient.<>c__DisplayClass1d 1.b__1c(任務1 responseTask) in c:\TeamCity\buildAgent\work\f1c4cf3efbf1b05e\Neo4jClient\GraphClient.cs:line 793 at System.Threading.Tasks.ContinuationResultTaskFromResultTask 2.InnerInvoke() 在System.Threading.Tasks.Task.Execute()

+0

滿堆棧跟蹤? FriendsWith的外觀如何? – 2013-05-08 06:55:16

+0

也許你錯過了類「FriendsWith」的默認(無參數)構造函數? – veljkoz 2013-05-08 15:05:28

+1

你可以添加一個默認的構造函數,你只需將一個虛擬值傳遞給基類構造函數,就像這樣:public FriendsWith():base(-1){} – 2013-08-27 13:16:17

回答

0

只是反序列化到一個POCO表示數據結構:

public class FriendsWith 
{ 
    public string __created { get; set; } 
} 

var query = _graph.Cypher 
    .Start(new { 
     me = Node.ByIndexLookup("node_auto_index", "id", p.id) 
    }) 
    .Match("me-[r:FRIENDS_WITH]-friend") 
    .Where((Person friend) => friend.id == f.id) 
    .Return(r => r.As<FriendsWith>()) 
    .Results; 

你其實並不需要FriendsWith : Relationship, IRelationshipAllowingSourceNode<Person>, IRelationshipAllowingTargetNode<Person>類在所有

。個

使用暗號創建關係:

_graph.Cypher 
    .Start(new { 
     me = Node.ByIndexLookup("node_auto_index", "id", p.id), 
     friend = Node.ByIndexLookup("node_auto_index", "id", p.id + 1) 
    }) 
    .CreateUnique("me-[:FRIENDS_WITH {data}]->friend") 
    .WithParams(new { data = new FriendsWith { __created = DateTime.Now.ToString("o") } }) 
    .ExecuteWithoutResults(); 

你會看到在Neo4jClient維基更多的例子。基本上,在這個時代,一切都應該是Cypher。

+1

他試圖查看或返回關係。不要創建一個新的。我也有同樣的問題。 – Frenchie 2014-03-27 14:27:57

+0

@Frenchie:我的答案的第一部分顯示了關係有效載荷的檢索。 – 2014-03-27 21:38:34

+0

@Frenchie:如果這不能解決你的具體情況,請提出一個新問題,以便我們討論它。 – 2014-03-27 21:38:56