2

Following is the code for which the error is being generated:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Neo4jClient;
using Neo4jClient.Cypher;

namespace ConsoleApplication1
{   
    public class SNode
    {
        public int shelfid { get; set; }
        public int floorid { get; set; }
    }

    public class PathsResult<TNode>
    {
        public IEnumerable<Node<TNode>> Nodes { get; set; }
        public IEnumerable<RelationshipInstance<object>> Relationships { get; set; }
    }

    class Program
    {
        public static PathsResult<SNode> getPath(int sourceShelfId, int destinationShelfId, GraphClient client)
        {
            var pathsQuery =
            client.Cypher
                .Match("p = shortestPath((src:Node)-[*..150]-(dest:Point))")
                .Where((SNode src) => src.shelfid == sourceShelfId)
                .AndWhere((SNode dest) => dest.shelfid == destinationShelfId)
                .Return(p => new PathsResult<SNode>
                {
                    Nodes = Return.As<IEnumerable<Node<SNode>>>("nodes(p)"),
                    Relationships = Return.As<IEnumerable<RelationshipInstance<object>>>("rels(p)")
                });

            var res = pathsQuery.Results;
            return res;
        }
    }
}

The error I'm receiving is that:

Cannot implicitly convert type System.Collection.Generic.IEnumerable<ConsoleApplication1.PathResult<ConsoleApplication1.SNode> > 
to ConsoleApplication1.PathResult<ConsoleApplication1.SNode> >. An explicit conversion exists, are you missing a cast?

From what I understand pathQuery.result should return a single PathResult object. However, I tried to make a cast according to the above error like this:

var res = pathsQuery.Results.AsEnumerable<PathsResult<SNode> >;

And now the new error it gives is:

Cannot assign method group to implicitly-typed local variable

Where am I going wrong?

Nitin Labhishetty
  • 1,290
  • 2
  • 21
  • 41
  • Did you forget the parenthesis? var res = pathsQuery.Results.AsEnumerable>(); – Kevin D Jun 10 '15 at 07:21
  • yes he has'nt put paranthesis of method – Ehsan Sajjad Jun 10 '15 at 07:24
  • The error says that you are trying to convert `IEnumerable` to `X`. So you need to do something like `.First()` where you do this. Since you didn’t provide the full error and told us where this is happening, we can’t really do much though. – poke Jun 10 '15 at 07:24
  • @poke: This is the full error and the full program. I tried adding the parenthesis and now getting the previous error. – Nitin Labhishetty Jun 10 '15 at 07:26

1 Answers1

2

You have several possibilites:

  1. change return type of getPath to IEnumerable...
  2. Use only first element of the query result: Add .FirstOrDefault()

    var res = pathsQuery.Results.FirstOrDefault();
    

Generally it is a good idea to set a breakpoint and put the mouse over var to inspect what type it is. Even better to avoid var in such cases and make the compiler report all problems.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • Thanks, that resolved the error, didn't think of using FirstOrDefault(). Just curious here, if I were to make a cast to resolve this error, how would I do that? – Nitin Labhishetty Jun 10 '15 at 07:38