返回重载失败 [英] Return overload fails

查看:22
本文介绍了返回重载失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这篇小文章:https://github.com/Readify/Neo4jClient/wiki/cypher 但我是从 Powershell 做的.所以到目前为止我所拥有的是

I'm following this little write up: https://github.com/Readify/Neo4jClient/wiki/cypher but I'm doing it from Powershell. so what I have so far is

[System.Reflection.Assembly]::LoadFrom("C:...Newtonsoft.Json.6.0.3lib
et40NewtonSoft.Json.dll")
[System.Reflection.Assembly]::LoadFrom("C:...Neo4jClient.1.0.0.662lib
et40Neo4jClient.dll")

$neo = new-object Neo4jClient.GraphClient(new-object Uri("http://localhost:7474/db/data"))
$q=$neo.Cypher.Match("n").Return({param($m) $m});

我的意思是检索数据库中的所有节点.示例中显示的 Return() 方法需要一个 lambda 表达式作为参数,这在 Powershell 中将是一个代码块,但是,我收到以下错误:

with which I would mean to retrieve all nodes in the database. the Return() method is shown in the example to require a lambda expression as a parameter, which in Powershell would be a code-block, however, I get the following error:

找不到Return"和参数计数的重载:1".在行:1 字符:1+ $q=$neo.Cypher.Match("n").Return({param($m) $m});+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (:) [], MethodException+ FullQualifiedErrorId : MethodCountCouldNotFindBest

Cannot find an overload for "Return" and the argument count: "1". At line:1 char:1 + $q=$neo.Cypher.Match("n").Return({param($m) $m}); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

我哪里出错了?

* 更新我 *

通过下面@PetSerAl 提供的解释,我已经设法更进一步,但我仍然被卡住了.下面我将引用写的 (c#),然后显示等效的 powershell.首先我们声明一个类

with the explanation provided by @PetSerAl below, I've managed to get a little further, but I'm still stuck. below I will quote the write up (c#) and then show the equivalent powershell. first we declare a class

public class User
{
    public long Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }
}

我的班级有点不同

Add-Type -TypeDefinition "public class Project { public string Code; public string Name; public string Parent; public string Lifespan; }"

他们的密码

MATCH (user:User)
RETURN user

他们的 C#

graphClient.Cypher
    .Match("(user:User)")
    .Return(user => user.As<User>())
    .Results

现在是我的密码

MATCH (n:Project)
RETURN n

...最后,我对 powershell 的尝试:

...and finally, my attempt at the powershell:

$exp = [System.Linq.Expressions.Expression]
$p = $exp::Constant("Project")
$fn = $exp::TypeAs($p, (new-object Project).GetType())
$return = $exp::Lambda([Func[Project]], $fn, $p)
$neo.Cypher.Match("n").Return($return)

但我收到一个错误

使用1"个参数调用Return"的异常:表达式必须构造为对象初始值设定项(例如:n => newMyResultType { Foo = n.Bar }),一个匿名类型初始值设定项(用于例如:n => new { Foo = n.Bar }),一个方法调用(例如:n =>n.Count()) 或成员访问器(例如:n => n.As().Bar).您不能提供代码块(例如:n => { var a = n + 1;返回一个;}) 或使用带参数的构造函数(例如:n => newFoo(n)).如果您使用 F#,还支持元组.参数名称:表达式"在行:1 字符:1+ $neo.Cypher.Match("n").Return($return)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullQualifiedErrorId : ArgumentException

Exception calling "Return" with "1" argument(s): "The expression must be constructed as either an object initializer (for example: n => new MyResultType { Foo = n.Bar }), an anonymous type initializer (for example: n => new { Foo = n.Bar }), a method call (for example: n => n.Count()), or a member accessor (for example: n => n.As().Bar). You cannot supply blocks of code (for example: n => { var a = n + 1; return a; }) or use constructors with arguments (for example: n => new Foo(n)). If you're in F#, tuples are also supported. Parameter name: expression" At line:1 char:1 + $neo.Cypher.Match("n").Return($return) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentException

这一次,实际上非常清晰易懂.所以我想要的是一个方法调用,例如<代码>n =>n.Count() 显然没有实现.

which, for once, is actually very clear and understandable. so what I want is a method call e.g. n => n.Count() and clearly did not achieve that.

帮助?

* 更新二 *

所以,继续从 powershell 走上 neo4j 的曲折之路,我尝试了@PetSerAl 的第二种方法,并取得了更进一步的进展.这是我设法写的:

so, continuing on with the torturous path of neo4j from powershell, I've given @PetSerAl's second approach a stab and got a little further. here's what I managed to write:

$neopath = "C:[...]Neo4jClient.dll"
Add-Type -ReferencedAssemblies $neopath -TypeDefinition @"
    using System;
    using System.Linq.Expressions;
    using Neo4jClient.Cypher;
    public class Project {
        public string Code;
        public string Name;
        public string Parent;
        public string Lifespan;
    };
    public static class NeoExp {
        public static readonly Expression<Func<Neo4jClient.Cypher.ICypherResultItem,Project>> GetProject = (n) => n.As<Project>();
}
"@

现在允许我这样做:

$neo.Cypher.Match("n:Project").Return([NeoExp]::GetProject)

奇迹般地,它起作用了!除了它没有给我带回任何数据:

and that, miraculously, works! except it brings me back no data:

Results ResultsAsync Query                         Client 
------- ------------ -----                         ------                                                                      
                     Neo4jClient.Cypher.CypherQ... Neo4jClient.GraphClient

而且我知道我在数据库中有项目......那么现在可能是什么问题?

and I know I have projects in the database... so what could the issue be now?

* 更新 III *

哇,如此接近但仍未完成.根据@PetSerAl 的最新建议.我试过了:

wow, so close but still not done. as per the latest suggestion from @PetSerAl. I tried:

$neo.Cypher.Match("n:Project").Return([NeoExp]::GetProject).get_Results()

产生了一个明显的错误:

which yielded an illuminating error:

使用0"参数调用get_Results"的异常:图表客户端未连接到服务器.首先调用 Connect 方法."

Exception calling "get_Results" with "0" argument(s): "The graph client is not connected to the server. Call the Connect method first."

这样就说明我首先需要做的是:

so that made it clear I first needed to do:

$neo.Connect()

此外,我需要在查询的匹配子句周围加上括号:

also, I needed parentheses around the query's match clause:

$neo.Cypher.Match("(n:Project)").Return([NeoExp]::GetProject)

现在我在 .Results 字段中按预期返回了 27 个结果......但是,结果都是空白的.所以我认为这可能与 n.As<Project>() 可能我的类定义不正确并且失败了.有什么想法吗?

now I get 27 results back as expected in the .Results field... however, the results are all blank. so I think maybe it has to do with the n.As<Project>() where perhaps my class isn't defined properly and that fails. any thoughts?

* 更新 IV *

好的,知道了.Project 类需要有属性,而不是字段:

ok, got it. the Project class needs to have properties, not fields:

    public class Project {
        public string Code { get; set; }
        public string Name { get; set; }
        public string Parent { get; set; }
        public string Lifespan { get; set; }
    };

就是这样.我有数据.是啊!!!

and that's it. I have data. yea!!!

@PetSelAl:我欠你一瓶啤酒

@PetSelAl: I owe you a beer

推荐答案

一百万感谢@PetSerAl,他很好地解释了问题的本质,并帮助我解决了问题.

a million thanks to @PetSerAl who explained the problem well in its essence, and who held my hand through its resolution.

重申一下,问题在于 Powershell 没有调用通用方法的内置机制,并且 Neo4jClient 库的 .Return 方法重载都是通用的.具体来说,Powershell 不提供提供方法类型的方法.

to restate, the problem is that Powershell has no built-in mechanism to call generic methods and the Neo4jClient library's .Return method overloads are all generic. Specifically, Powershell provides no means of supplying the type of the method.

所以有几种方法可以解决这个问题:

so there are several ways to solve this:

@PetSerAl 的回复中建议的第二个解决方案需要一点帮助,但在 @ChrisSkardon 的帮助下,我让它发挥了作用.请参阅此处:构造方法调用

the second solution suggested in @PetSerAl's reply needed a little help but with @ChrisSkardon's help I got it to work. see here: Constructing a method call

和第三个解决方案(请参阅原始帖子中的更新),该解决方案依赖于使用可以调用泛型方法的方法创建 c# 类

and the third solution (see the Updates in the original post) which relies on creating a c# class with a method where the generic method can be called

我希望在 Neo4jClient 的 wiki 中记录此解决方案,我希望此处记录的工作对其他人有所帮助.再次感谢@PetSerAl 的帮助

I expect to be documenting this solution in the wiki for Neo4jClient and I hope the effort documented here is helpful to others. Thanks again @PetSerAl for all your help

这篇关于返回重载失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆