2016-03-15 62 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

您的第一个解决方案是我所需要的,因为它保持了查询的动态性质,非常感谢! –