Returning multiple columns in Neo4jClient Cypher Query

喜你入骨 提交于 2019-12-04 17:35:31

I think what you're missing here is that the Return statement returns one object per Cypher row.

Your query is returning a table like this:

|-----------------|
|    n   |   m    |
|-----------------| 
|  Node  |  Node  |
|------------------

That's one table, with one row, with two columns.

In this statement, you are returning an anonymous type per Cypher row:

.Return((n, m) => new
{
    N = n.CollectAs<Node<Item>>(),
    M = m.CollectAs<Node<Item>>()
});

The return type of that method is IEnumerable<AnonymousType>.

You're then trying to get the first row (an anonymous type) and implicitly cast that to Node<Item>, which is not valid.

You should get the row, then get the properties within it.

Some other things to note:

  • You don't want to use CollectAs in this scenario: that will turn each cell of your table into an array with a single value, which just adds more indirection.
  • .As<Node<T>>() can be written as .Node<T>()

With that in mind, here's the query you want:

var result = _graphClient
    .Cypher
    .Start(new
    {
        n = Node.ByIndexLookup("item_idx", "SKU", sSKU1),
        m = Node.ByIndexLookup("item_idx", "SKU", sSKU2),
    })
    .Return((n, m) => new
    {
        N = n.Node<Item>(),
        M = m.Node<Item>()
    })
    .Results
    .Single();

var n = result.N;
var m = result.M;

Make sense?

PS: I hope you aren't actually clubbing anything. Baby seals don't like that. You are combining queries.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!