var statementText var statementParameters = 这两句不是一般的调用diver里面的run方法,这个是什么操作可以传参进去吗,就是指定查询那部电影
发布于 5 年前 作者 ws0906 1441 次浏览 来自 问答

namespace Neo4jDotNetDemo.Controllers { using System.Collections.Generic; using System.Web.Http; using Neo4j.Driver.V1;

[RoutePrefix("graph")]
public class GraphController : ApiController
{
    [HttpGet]
    [Route("{limit:int?}", Name = "getgraph")]

    //public GraphController(string uri, string user, string password)
    //{
    //    WebApiConfig.Neo4jDriver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
    //}                                 //bolt协议的url  用户名密码字符串     2.指定的url以及授权模式


    public IHttpActionResult Index(int limit = 10, string q = "Matrix")
    {
        //query = ("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) "
        // "RETURN m.title as movie, collect(a.name) as cast "
        // "LIMIT {limit}")

        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>();

        using (var session = WebApiConfig.Neo4jDriver.Session(AccessMode.Read))
        {
            var result = session.Run(statementText, statementParameters);

            var i = 0;

            foreach (var record in result)
            {
                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 });
                }
            }
        }

        return Ok(new {nodes, links = relationships});
    }
}

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; }
    public int born { get; set; }
   
}

}

想请问一下 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}}; 这两句不是一般的调用diver里面的run方法,这个是什么操作可以传参进去吗,就是指定查询那部电影

1 回复

第一行{limit}就是参数占位符 第二行 用Dictionar封装了 参数 列表,其中就含有一项{“limit”,limit},就是第一行的参数在这里赋值。

回到顶部