Can';无法从Neo4jClient C中的查询中获取属性#
这是我第一次尝试使用Neo4jClient,但没有任何经验。 我希望我的程序能够打印出Neo4j中指定的具有特定关系的人员的姓名。 我有非常简单的代码,如:Can';无法从Neo4jClient C中的查询中获取属性#,neo4j,neo4jclient,Neo4j,Neo4jclient,这是我第一次尝试使用Neo4jClient,但没有任何经验。 我希望我的程序能够打印出Neo4j中指定的具有特定关系的人员的姓名。 我有非常简单的代码,如: using Neo4jClient; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespa
using Neo4jClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Neo4J.NET_Labs
{
class Program
{
static void Main(string[] args)
{
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
var query = client
.Cypher
.Match("(n)-[:LOVE]-(lover)")
.Return(lover => lover.As<Person>())
;
int count = 0;
foreach (var result in query.Results)
{
count++;
Console.WriteLine("People {0} count {1}", result.name, count);
}
//Stop to show result
Console.ReadLine();
}
}
public class Person
{
public string name;
}
有人能告诉我如何从query.Result获取属性吗。这是因为“Person”类的定义,JSON.Net无法反序列化到成员,需要属性。您可以通过运行以下代码来测试这一点:
public class Person { public string Name { get;set;} }
public class PersonWithMembers { public string Name; }
基本相同的类,一个具有属性,一个具有成员
//Stick this in the Main method
var pA = new Person {Name = "PersonA", Twitter = "tA"};
var pB = new Person {Name = "PersonB", Twitter = "tB"};
gc.Cypher.Create("(p:Person {person})").WithParam("person", pA).ExecuteWithoutResults();
gc.Cypher.Create("(p:Person {person})").WithParam("person", pB).ExecuteWithoutResults();
Console.WriteLine("Members:");
var membersQuery = gc.Cypher
.Match("(p:Person)")
.Return(p => p.As<PersonWithMembers>());
foreach (var p in membersQuery.Results)
Console.WriteLine(p.Name);
Console.WriteLine("Properties:");
var propertiesQuery = gc.Cypher
.Match("(p:Person)")
.Return(p => p.As<Person>());
foreach (var p in propertiesQuery.Results)
Console.WriteLine(p.Name);
该代码应该可以正常工作。您能否直接在基于web的浏览器()中运行查询,并查看数据库中的数据是否完全不同?亲爱的@TathamOddie,是的,我使用Cypher运行了查询:“匹配(a)-[:LOVE]-(b)返回a.name,b.name”,并看到出现了两个具有LOVE关系的节点。我使用的是Neo4j 2.0 RC1。OS win7-64 NET Framework 4.5。您能将您的“名称”更改为属性吗?因此它应该是
公共字符串名称{get;set;}
我相信Json.NET需要属性,而不是成员才能工作…啊。抢手货我很少使用公共字段做任何事情,所以我甚至没有想到这一点。这一点以前也提过一次。如果它再次出现,我可能会添加一个“嘿,您在该类型上有0个属性,是否要再次尝试编写该代码?”异常。嘿,如果看到弹出窗口,这将是一个非常好的异常:)
//Stick this in the Main method
var pA = new Person {Name = "PersonA", Twitter = "tA"};
var pB = new Person {Name = "PersonB", Twitter = "tB"};
gc.Cypher.Create("(p:Person {person})").WithParam("person", pA).ExecuteWithoutResults();
gc.Cypher.Create("(p:Person {person})").WithParam("person", pB).ExecuteWithoutResults();
Console.WriteLine("Members:");
var membersQuery = gc.Cypher
.Match("(p:Person)")
.Return(p => p.As<PersonWithMembers>());
foreach (var p in membersQuery.Results)
Console.WriteLine(p.Name);
Console.WriteLine("Properties:");
var propertiesQuery = gc.Cypher
.Match("(p:Person)")
.Return(p => p.As<Person>());
foreach (var p in propertiesQuery.Results)
Console.WriteLine(p.Name);
Members:
Properties:
PersonA
PersonB