sql:
MATCH (p:Person) RETURN p.name AS name
/// <summary>
/// http://localhost:7474/browser/
/// D:\neo4j\neo4j-community-5.26.0\bin>neo4j.bat console
/// geovindu,Geovin Du
/// </summary>
public class NodeResult
{
public string title { get; set; }
public string label { get; set; }
}
public class Movie
{
public string title { get; set; }
public int released { get; set; }
public string tagline { get; set; }
}
public class Person
{
public string Name { get; set; } = "";
}
/// <summary>
/// https://github.com/neo4j/neo4j-dotnet-driver
///https://github.com/neo4j-examples/movies-dotnetcore-bolt
///https://github.com/neo4j-examples/movies-dotnet-bolt
///https://neo4j.com/docs/dotnet-manual/current/session-api/
/// </summary>
public class Neo4jHelper
{
/// <summary>
///
/// </summary>
private IDriver _driver;
/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <param name="user"></param>
/// <param name="password"></param>
public Neo4jHelper(string uri, string user, string password)
{
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
}
/// <summary>
///
/// </summary>
public void close()
{
_driver.CloseAsync();
_driver.Dispose();
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task PrintGreetingAsync(string message)
{
var session = _driver.AsyncSession();
var greeting = await session.ExecuteWriteAsync(
async tx =>
{
var result = await tx.RunAsync(
"CREATE (a:Greeting) " +
"SET a.message = $message " +
"RETURN a.message + ', from node ' + id(a)",
new { message });
var record = await result.SingleAsync();
return record[0].As<string>();
});
Console.WriteLine(greeting);
}
/// <summary>
/// 查询
/// </summary>
/// <returns></returns>
public async Task<List<IRecord>> getQuery()
{
List<IRecord> record = new List<IRecord>();
//var data= _driver.ExecutableQuery("MATCH (p:Person) RETURN p.name AS name");
var session = _driver.AsyncSession();
//var exeResult = session.ExecuteWriteAsync(async x =>
//{
// var result = await x.RunAsync("MATCH (p:Person) RETURN p.name AS name");
// return await result.ToListAsync();
//});
//await exeResult;
// or:
// exeResult.Wait();
var exeResult = session.ExecuteWriteAsync(async x =>
{
var result = await x.RunAsync("MATCH (p:Person) RETURN p.name AS name");
//return await result.ToListAsync();
record = await result.ToListAsync();
});
try
{
await exeResult;
}
catch (ClientException ex)
{
Console.WriteLine(ex);
}
return record;
}
/// <summary>
///
/// </summary>
/// <param name="limit"></param>
/// <returns></returns>
public async Task getQuerylist(int limit = 100)
{
//string dbConfig = "";
//var(result, _, _) = await _driver.ExecutableQuery(query).ExecuteAsync(); //.WithConfig(new QueryConfig(database: "neo4j")).ExecuteAsync()
// IExecutableQuery<records,summary> a= _driver.ExecutableQuery(query);
// IExecutableQuery<IRecord, IRecord> aa = ExecutableQuery(string cypher);
// return records;
//return result;
var statementText = "MATCH (a:Person)-[:ACTED_IN]->(m:Movie) RETURN m.title as movie, collect(a.name) as cast LIMIT {limit}";
var statementParameters = new Dictionary<string, object> { { "limit", limit } };
var nodes = new List<NodeResult>();
var relationships = new List<object>();
// _driver.AsyncSession();
using (var session = _driver.AsyncSession())
{
var result = await session.RunAsync(statementText, statementParameters);
var i = 0;
var records= await result.ToListAsync();
foreach (var record in records)
{
var target = i;
nodes.Add(new NodeResult { title = record["movie"].As<string>(), label = "movie" });
i += 1;
var castMembers = record["cast"].As<List<string>>();
foreach (var castMember in castMembers)
{
var source = nodes.FindIndex(c => c.title == castMember);
if (source == -1)
{
nodes.Add(new NodeResult { title = castMember, label = "actor" });
source = i;
i += 1;
}
relationships.Add(new { source, target });
}
}
//result.ToListAsync();
}
}
}
调用
Neo4jHelper helper = new Neo4jHelper("bolt://localhost:7687", "neo4j", "geovindu");
var a= await helper.getQuery();
Response.Write(a.Count());
foreach (var record in a)
{
Response.Write(record["name"].ToString()+"<br/>");
//Response.Write(record[0].ToString() + "<br/>");
}
输出: