问题描述
我有一个查询,它返回一个带有属性的对象列表。考虑一个具有类似结构的 C# 对象:
public class Neo4jResult {
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}
查询返回一个名为“mycollection”的列,我可以将结果存储为这样的:
public async Task<IEnumerable<Neo4jResult>> MyNeo4jQuery() {
var cypher = client.Cypher
.Match(matchQuery)
.WithParams(myParams);
cypher =
cypher.Returndistinct<Neo4jResult>("mycollection")
.OrderBy("toLower(object.Prop1)");
var query = (IOrderedCypherFluentQuery<Neo4jResult>)cypher;
return await query.ResultsAsync;
}
此代码运行良好。但是,我必须将此记录的计数作为另一个属性,因此我的查询现在返回两列 - “mycollection”和“totalRecords”。为方便起见,我创建了一个反映这一点的新对象:
public class Neo4jResultNew {
public int TotalRecords { get; set; }
public IEnumerable<Neo4jResult> Results { get; set; }
}
然后我将我的 Neo4j 查询更改为类似:
public async Task<IEnumerable<Neo4jResult>> MyComplexNeo4jQuery() {
var cypher = client.Cypher
.Match(matchQuery)
.WithParams(myParams);
cypher =
cypher.Return<Neo4jResultNew>( (mycollection,totalRecords) => {
{
Results = mycollection.As<IEnumerable<Neo4jResult>>(),TotalRecords = totalRecords.As<int>()
});
var query = (IOrderedCypherFluentQuery<Neo4jResultNew>)cypher;
return await query.ResultsAsync;
}
neo4j 返回的错误是:“Neo4j 返回了有效的响应,但是 Neo4jClient 无法反序列化为您提供的对象结构”。我只是按照在线示例进行操作,但我的投影中可能缺少某些内容?
解决方法
我认为是你的问题:
Results = mycollection.As<IEnumerable<Neo4jResult>>(),
位。你真正想要的是一个 COLLECT
像这样:
Results = mycollection.CollectAs<Neo4jResult>()
mycollection
不是实际上一个 IEnumerable
- 如果你在浏览器中运行查询你可以看到它 - 你没有把它放在这里,所以这个是一个“粗略”版本。
如果你执行了:
MATCH (m:Movie)
RETURN m.title,count(m)
你会得到:
Title1,1
Title2,1
Title3,1
等
如果你执行:
MATCH (m:Movie)
RETURN COLLECT(m.title),count(m)
你会得到:
[title1,title2,title3],3
例如。