1

I'm trying to assign data from Neo4j database to a variable in C# according to available examples like so:

    var born = graphClient.Cypher
            .Match("(person:Person)")
            .Where((Person person) => person.name == "Tom Hanks")
            .Return(person => person.As<Person>().born)
            .Results;

But when i try to print the value out :

    Console.WriteLine(born);

I get this in console :

    System.Collections.Generic.List`1[System.Int32]

What I'm doing wrong?

Karas
  • 15
  • 4
  • How do you want to print values of `born` variable? do you want to print it in single line, if yes then do you want to print comma separated? if no then do you want to print each value on new line? – Prasad Telkikar Jan 30 '20 at 16:40
  • Since no one appears to be addressing the "issue" you're seeing, I just wanted to comment and say the reason you're getting "System.Collections.Generic.List`1[System.Int32]" printed to the console is because the default ToString() method for System.Collections.Generic.List returns it. If you like, you could create a new class that derives from System.Collections.Generic.List and override the ToString() method to have it return what you want. – aardvark Jan 30 '20 at 16:51
  • Does this answer your question? [Unable to print whole list to console](https://stackoverflow.com/questions/52737146/unable-to-print-whole-list-to-console) or [I got a problem with a simple List in C#(not displaying the actually list)](https://stackoverflow.com/q/56536296/150605) or [How to display list items on console window in C#](https://stackoverflow.com/q/759133/150605) – Lance U. Matthews Jan 30 '20 at 20:40

3 Answers3

0

As born refers to a list, you will need to iterate over the items:

foreach (int item in born)
{
    Console.WriteLine(item);
}

Console.WriteLine calls ToString() on the object that is passed, which for most types will be the type name, as you are observing.

When you iterate as above, it will write the string representation of each integer in the list.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • How do i print using foreach in this situation? var about = graphClient.Cypher .OptionalMatch("(person:Person)-[ACTED_IN]->(movie:Movie)") .Where((Person person) => person.name == "Tom Hanks") .Return((person, movie) => new { Person = person.As().name, Date = person.As().born, NumberOfRoles = movie.Count() }) .Results; When I want to return more than one value? – Karas Jan 30 '20 at 16:42
  • @Karas Is this for debugging purposes? If so, you would be better to use the [debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/?view=vs-2019). – Johnathan Barclay Jan 30 '20 at 16:48
  • No, I want to use the data, I return 3 values and want to use each in different situations – Karas Jan 30 '20 at 17:18
0

From your output, you are receiving in results a collection of items which match your filter. If you want to print the first element, you can do something like:

Console.WriteLine(born.First());

Just keep in mind that in that collection it could be more than one element...

If you want to print all the born items, you can use a foreach loop to do so: You only have to do something like:

foreach (var item in born)
{
    Console.WriteLine(item);
}

the use of the foreach loop is the best approach because even when you have 0 elements in the collection, it works as expected (i.e: it doesn't fail or throw an exception) You have to be careful with the use of the First() there because it will throw an exception if your collection is empty. So if you know that you can end up with an empty collection you can always use FirstOrDefault() which return null - or the default value according to the data type - in case of an empty collection

Guillermo Gerard
  • 808
  • 10
  • 21
  • How to use foreach in this situation : `var about = graphClient.Cypher .OptionalMatch("(person:Person)-[ACTED_IN]->(movie:Movie)") .Where((Person person) => person.name == "Tom Hanks") .Return((person, movie) => new { Person = person.As().name, Date = person.As().born, NumberOfRoles = movie.Count(), Movie = movie.As().title }) .Results;` When I return more than 1 value? – Karas Jan 30 '20 at 17:25
  • @Karas: I've edited my answer with more information about the use of the foreach loop and the LINQ's First() method. I hope you find it useful – Guillermo Gerard Jan 30 '20 at 20:32
0

either serialize your "born" collection and print or go one by one object from that "born" list and print

Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(born));
divyang4481
  • 1,584
  • 16
  • 32