1

Right I have a really simple problem, but I cannot for the life of me think of the really simple answer to go with it. This code is supposed to return a single 'Person', with a collection of languages and countries.

return client.Cypher
            .Match("(person:Person)")
            .Where((Person person) => person.Email == username)
            .OptionalMatch("(person)-[:SPEAKS]-(language:Language)")
            .OptionalMatch("(person)-[:CURRENT_LOCATION]-(country:Country)"
            .Return((person, language, country) => new ProfileObject
            {
                Person = person.As<Person>(),
                Language = language.CollectAs<Language>(),
                Country = country.CollectAs<Country>()
            }).Results.ToList();

It looks right to me, but it isn't, on build I get this error, which I understand but cannot solve.

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Neo4jClient.Node<Graph.Country>>' to 'Graph.Country'. An explicit conversion exists (are you missing a cast?)

The Language class looks like this

public class Language
{
    public string Name { get; set; }
}

And the ProfileObject class looks like this:

public class ProfileObject
{
    public Person Person { get; set; }
    public Language Language { get; set; }
    public Country Country { get; set; }
}

I am really stuck, please help.

Shaine Fisher
  • 315
  • 1
  • 3
  • 20

1 Answers1

2

CollectAs returns a set of nodes.

You need to change your ProfileObject to:

public class ProfileObject
{
    public Person Person { get; set; }
    public IEnumerable<Node<Language>> Language { get; set; }
    public IEnumerable<Node<Country>> Country { get; set; }
}

In a forthcoming update to the package, the Node<T> wrapper has been removed from the signature, so it will just be:

public class ProfileObject
{
    public Person Person { get; set; }
    public IEnumerable<Language> Language { get; set; }
    public IEnumerable<Country> Country { get; set; }
}

If you want to get that cleaner signature now, check out the pre-release packages on NuGet (https://www.nuget.org/packages/Neo4jClient/1.1.0-Tx00009).

Tatham Oddie
  • 4,290
  • 19
  • 31
  • Although that did solve the problem asked in the question, it did raise another question. This method is being called through a webservice, the additional IEnumerable> throws an error. Unable to serialize because Country is an interface, any really good suggestions for that? – Shaine Fisher Jul 25 '15 at 19:06
  • It seems I have hit a limitation, or a mental block, this thread (http://stackoverflow.com/questions/31630614/cannot-serialize-member-x-because-it-is-an-interface) summarises where it is going wrong. We found a way to serialize the result, but we get an error 'Neo4jClient.Node`1[Graph.Language] cannot be serialized because it does not have a parameterless constructor.' and the error *appears* to be that the Neo4jClient.Node is not parameterless, and cannot be made so. – Shaine Fisher Jul 26 '15 at 10:09
  • And if anyone finds this thread with a similar problem, Tatham's answer above is brilliant, but if you get serialization errors after that then this thread: http://stackoverflow.com/questions/31630614/cannot-serialize-member-x-because-it-is-an-interface/31631393 may fill in the rest of the solution. Excellent work from these people, really. – Shaine Fisher Jul 26 '15 at 19:31