简体   繁体   English

查询 Neo4j 并在 WPF 应用程序中使用 Net Driver 返回找到的节点

[英]Query Neo4j and return found Nodes with Net Driver in WPF App

I like to search in my neo4j db with a string and get all Nodes as results back to handle them later in an AutosuggestBox.我喜欢用字符串在我的 neo4j 数据库中搜索,然后将所有节点作为结果返回,以便稍后在 AutosuggestBox 中处理它们。

First throw was to bring up a connection and query then the db to return my Nodes.第一次抛出是建立一个连接并查询然后数据库返回我的节点。

Now Im having trouble to return each found Node with its attribute called Name if its match, im always getting strange results in my messagebaox where I try to check my returned Nodes:现在我无法返回每个找到的节点,如果匹配,则返回其名为 Name 的属性,我总是在我的 messagebaox 中得到奇怪的结果,我尝试检查返回的节点:

        private static async Task<List<string>> GetSuggestionsFromNeoAsync(string currentInput) {

            IDriver driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "neo4j"));
            IAsyncSession session = driver.AsyncSession(o => o.WithDatabase("neo4j"));

            var query2Neo = "Match (p:Person { Name:'" + currentInput + "'}) RETURN p.samAccountName as name";

            IResultCursor cursor = await session.RunAsync(query2Neo);
            List<string> people = await cursor.ToListAsync(record => record["Name"].As<string>());

            await cursor.ConsumeAsync();

            await driver.CloseAsync();
            return people;
    }

Ive tryed to setup that Query to get Reults like from: https://github.com/neo4j/neo4j-dotnet-driver But most exmaples are for neo4j v3 with the Client.我已经尝试设置该查询以从以下位置获取 Reults: https : //github.com/neo4j/neo4j-dotnet-driver但大多数示例适用于客户端的 Neo4j v3。

thank you and best regards感谢你并致以真诚的问候

update with working code:使用工作代码更新:

        private static async Task ShowResultsAsync()
    {
        var found = await GetSuggestionsFromNeoAsync("Name");
        if (found.Count > 0)
        {
            //enumerate the inner list
            foreach (var item in found)
            {
                //output the actual item
                MessageBox.Show("Person found Entry: " + item);
            }
        }
        else {
            MessageBox.Show("Person not found");

        }

    }

GetSuggestionsFromNeoAsync is an asynchronous method. GetSuggestionsFromNeoAsync是一种异步方法。 This mean that it will return when it hits an await and execute the remainder of the method when the Task being awaited has completed.这意味着它将在遇到await时返回,并在awaitTask完成时执行方法的其余部分。

So when you call GetSuggestionsFromNeoAsync from your showREsults method, it runs synchronously until the IResultCursor cursor = await session.RunAsync(query2Neo) line and then returns, whereupon the MessageBox is shown.因此,当您从showREsults方法调用GetSuggestionsFromNeoAsync时,它会同步运行,直到IResultCursor cursor = await session.RunAsync(query2Neo)行然后返回,然后显示MessageBox Please refer to the docs for more information about what happens in an async method.有关异步方法中发生的事情的更多信息,请参阅文档

If you want to display the MessageBox when the GetSuggestionsFromNeoAsync has completed, you should await the async method.如果要在GetSuggestionsFromNeoAsync完成时显示MessageBox ,则应等待异步方法。 For you to be able to do this, you need to add the async keyword to the signature of the showREsults method.为了能够做到这一点,您需要将async关键字添加到showREsults方法的签名中。 You should also change its return type to Task and add the *Async suffix to its name:您还应该将其返回类型更改为Task并在其名称中添加 *Async 后缀:

private static async Task ShowResultsAsync()
{
    var found = await GetSuggestionsFromNeoAsync("Name");
    MessageBox.Show("Person found Entry: " + found.Name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM