2016-03-15 38 views
0

在動態查詢返回多值之後,我問了一個問題:Build a dynamic query using neo4j client使用Neo4j的客戶

我得到了我如何動態僅僅使用了字符串返回值的答案。

當我嘗試使用語法從失敗的查詢返回多值,
我嘗試以下查詢:

var resQuery2 = WebApiConfig.GraphClient.Cypher 
      .Match("(movie:Movie {title:{title}})") 
      .OptionalMatch("(movie)<-[r]-(person:Person)") 
      .WithParam("title", title) 
      .Return(() => Return.As<string>("movie, collect([person.name, head(split(lower(type(r)), '_')), r.roles])")); 

,我發現了以下錯誤:

The deserializer is running in single column mode, but the response included multiple columns which indicates a projection instead. If using the fluent Cypher interface, use the overload of Return that takes a lambda or object instead of single string. (The overload with a single string is for an identity, not raw query text: we can't map the columns back out if you just supply raw query text.)

是否有可能僅使用字符串返回多個節點?

回答

1

我們不能得到一個輸出像以前問的問題 - 這是由於這樣的事實,你所要求的一個節點(movie)和字符串(該collect)的集合,它們沒有共同的財產,甚至財產的風格。

首先,讓我們來看看在痛苦的方式做到這一點:

var q = gc.Cypher 
    .Match("(movie:Movie)") 
    .OptionalMatch("(movie)<-[r]-(person:Person)") 
    .Return(() => Return.As<string>("{movie:movie, roles:collect([person.name, head(split(lower(type(r)), '_')), r.roles])}")); 

var results = q.Results; 

下面我們就來查詢項目(movie, r, person),並創建一種與周圍的結果{},並強制轉換成一個string

這會給你一個可怕的字符串與movie圍繞Node數據,然後將角色的集合:

foreach (var m in results) 
{ 
    //This is going to be painful to navigate/use 
    dynamic d = JsonConvert.DeserializeObject<dynamic>(m); 
    Console.WriteLine(d.movie); 
    Console.WriteLine(d.roles); 
} 

你會好很多關做這樣的事情:

var q = gc.Cypher 
    .Match("(movie:Movie)") 
    .OptionalMatch("(movie)<-[r]-(person:Person)") 
    .Return(() => new 
    { 
     Movie = Return.As<Node<string>>("movie"), 
     Roles = Return.As<IEnumerable<string>>("collect([person.name, head(split(lower(type(r)), '_')), r.roles])") 
    }); 

    var res = q.Results; 

你可以在電影節點JsonConvert.DeserializeObject<dynamic>(),閒暇時間,或寫一個強類型的類。

就「動態」對象而言,我不知道如何與返回語句的collect部分進行交互,如果這沒有幫助,則可能需要更新問題以顯示使用期望。

+0

您的第一個解決方案是我所需要的,因爲它保持了查詢的動態性質,非常感謝! –