2014-06-27 93 views
1

很難問這個問題,即使我不確定我是否選擇了正確的標題。但請裸露在我身上。neo4jclient:創建並將多個節點關聯到另一個節點

在C#中想象一下類:

public class User 
    { 
     public int Id { get; set; } 
     public string Username { get; set; } 
    } 

public class Post 
{ 
    public int Id { get; set; } 
    public string Text { get; set; } 
    public string[] HashTags { get; set; } 
} 

每個用戶可以添加後,它們之間的關係將是作者,每一個職位可能有哈希標籤的陣列,他們每個人將要在圖中是一個單獨的節點。

當我要保存每個帖子時,我會在grph中找到用戶,創建帖子節點並將它們與作者關係關聯。

問題是如何創建每個hashTag並將其與相同查詢中的帖子相關聯。 (在交易中)。

我怎樣才能動態地添加項目來查詢來創建它。 問題在於它無法在一行創建中創建節點和關係。

這裏是我到目前爲止已經試過:

var cypherQuery = Db.Instance.Cypher 
       .Match("(user:User)") 
       .Where((User user) => user.Username == "XYZ") 

       .Create("user-[:Author]->(post:Post {newPost})") 
       .WithParam("newPost", new Post() {Id = 1, Text = "Here is my post about #someHashTag"}); 
       //How to relate this node to the number of hashTags in Post Object??? 
       cypherQuery.ExecuteWithoutResults(); 

是好是在單個查詢或者我應該把它在多次往返。

我試圖與foeach的東西,但它接縫,該文章沒有foreach循環內的任何值:

我已經試過這樣的事情:

var cypherCommand = Db.Instance.Cypher 
       .Match("(user:User)") 
       .Where((User user) => user.Username == "farvashani") 

       .Create("user-[:Author]->(post:Post {newPost})") 
       .WithParam("newPost", "here is my post about @Tag1 and Tag2") 
       .ForEach(@"(hashtag in {hashTags}| 
          MERGE post-[:Mentioned]->(hash:HashTag {Text: hashtag}))") 
       .WithParam("hashTags", new string[] {"Tag1", "Tag2"}); 
       cypherCommand.ExecuteWithoutResults(); 

任何幫助,將不勝感激。

回答

1

在我看來,我認爲你必須先預處理Text屬性。

String text = ""Here is my post about #someHashTag""; // For example 
List<String> hashTags = new List<String>(); 
int cnt = 0; 
foreach (Match match in Regex.Matches(text, @"(?<!\w)#\w+")) 
    { 
     hashTags.Add(match.Value); 
    } 

然後創建後的新實例:

Post newPost = new Post 
     { 
      Id = 1, 
      Text = "Here is my post about #someHashTag", 
      hashTags = hashTags 
     }; 

所以,你可以使用這個暗號:

var cypherCommand = Db.Instance.Cypher 
      .Match("(user:User)") 
      .Where((User user) => user.Username == "farvashani") 

      .Create("user-[:Author]->(post:Post {newPost})") 
      .WithParam(new {newPost}).ExecuteWithoutResults(); 

希望這有助於。

P/s:我可以問你一個問題嗎?如果您使用每個hashTag作爲Post的標籤,您是否認爲檢索分隔圖更好? newPost:someHashTag例如?

相關問題