简体   繁体   English

Neo4j密码查询到使用WITH的C#

[英]Neo4j Cypher Query To C# using 'WITH'

I want to get client who has two accounts, one is Type1 and another is Type2. 我想获得拥有两个帐户的客户,一个是Type1,另一个是Type2。 Cygher query is as below Cygher查询如下


MATCH (client:Client)-[:OWNED_BY]-(account:Account)-[:TYPE_IN]-(type:Type) 
With client, account, type
MATCH (client)-[:OWNED_BY]-(account1:Account)-[:TYPE_IN]-(:Type{accountType: 'Type1'}) 
,(client)-[:OWNED_BY]-(account2:Account)-[:TYPE_IN]-(:Type{accountType: 'Type2'})
RETURN client, account, type

it works(any enhancement is welcome, but this is not my issue ) 它有效(欢迎任何增强功能,但这不是我的问题)

How I can convert it into C#? 如何将其转换为C#? particularly 'With client, account, type' part. 特别是“与客户,帐户,类型”部分。 I had Neo4j.client 我有Neo4j.client

I managed to do this, 我设法做到了


StringBuilder queryString =new StringBuilder();
            StringBuilder addtionQuery = new StringBuilder();

            queryString.Append("(client:Client)-[:OWNED_BY]-(account:Account)-[:TYPE_IN]-(type:Type)");

            addtionQuery.Append(String.Format("(client)-[:OWNED_BY]-(account0:Account)-[:TYPE_IN]-(:Type{{accountType: '{0}'}})"
                , searchCriteria.acctTypes[0]));

            for (int i = 1; i < searchCriteria.acctTypes.Length; i++)
            {
                if (searchCriteria.acctTypes[i] != null)
                {
                    addtionQuery.Append(",");
                    addtionQuery.Append(String.Format("(client)-[:OWNED_BY]-(account{1}:Account)-[:TYPE_IN]-(acct_type{1}:Type{{accountType: '{0}'}})", 
                        searchCriteria.acctTypes[i], i));
                }
            }


var query = Dbclient.Cypher
                .OptionalMatch(queryString.ToString())
                .With((client, account, type) => new 
                {
                    client = client.As<Client>(),
                    type = type.As<Demo1.Models.Type>(),
                    account = account.As<Account>()
                })
                .OptionalMatch(addtionQuery.ToString())
                .Return((client, account, type) => new QueryResult
                {
                    cif = client.As<Client>().cif,
                    type = type.As<Demo1.Models.Type>().accountType,
                    accountId = account.As<Account>().accountId
                })
                .Limit(searchCriteria.limit)
                .Results;

it works but result is not correct 它有效,但结果不正确

I can't help you with the c# neo4jclient API, but your statement might be more efficient like this: 我无法通过c#neo4jclient API帮助您,但是您的语句可能会像这样更高效:

MATCH (client:Client)-[:OWNED_BY]-(account:Account)-[:TYPE_IN]-(type:Type) 
WHERE type.accountType IN ['Type1','Type2']
With client, collect([account, type]) as accounts
WHERE size(accounts) > 1
RETURN client, accounts

Well, your queries are different. 好吧,您的查询是不同的。 Your Cypher query has MATCH clauses while your Neo4jClient query has OPTIONAL MATCH clauses, and you have different names for your nodes (account, account1, account2, ... <-> account, account0, ...). Cypher查询具有MATCH子句,而Neo4jClient查询具有OPTIONAL MATCH子句,并且节点具有不同的名称(account,account1,account2,... <-> account,account0,...)。

I suggest you make sure that both queries are the same. 我建议您确保两个查询都相同。 You can use a debugger or print the resulting Cypher query with query.Query.QueryText or query.Query.DebugQueryText . 您可以使用调试器,也可以使用query.Query.QueryTextquery.Query.DebugQueryText打印生成的Cypher查询。 In this case query is what you get before you call .Results on it, but your variable name is a bit off. 在这种情况下, query是在调用.Results之前获得的.Results ,但是您的变量名有点过头。

As far as WITH is concerned, the With() method can take a string as a parameter too, so you can simply write: WITH而言, With()方法也可以将string作为参数,因此您可以简单地编写:

.With("client, account, type")

IMO, The With() that takes a lambda offers little or no gain over the one that takes a string , it's just more noise. IMO,采用lambda的With()与采用string ,提供的收益很少或没有提供,只是更多的噪音。

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

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