Cannot implicitly convert type from System.Collection.Generic.IEnumerable.MyClass<Node> to MyClass<Node>

299 views Asked by At

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?

1

There are 1 answers

1
DrKoch On BEST ANSWER

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.