Cannot convert implicitly type "System.Collections.Generic.Ienumberable<SS_API.Model.Project>"

Asked

Viewed 217 times

2

Cannot convert type implicitly "System.Collections.Generic.IEnumberable<SS_API.Model.Project>" em "Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.Inumerable<string>>".

How could I make the class call on controller correctly?

Interface:

    public interface IAcess : IDisposable
    {
        IEnumerable<Project> GetProjects();
        Project GetProject(int id);
        void AddProject(Project item);
        void DeleteProject(int id);
        void UpdateProject(Project item);
        void Save();

    }

Class:

  public class Acess : IAcess
    {
        private Project project = new Project();
        private StreamerContext context;
        public Acess(StreamerContext streamer)
        {
           this.context = streamer;
        }
        public IEnumerable<Project> GetProjects()
        {
          

                return context.Project.ToList();
                    
        }
}

Controller:

        public ActionResult<IEnumerable<string>> Get()
        {

            var projects = from project in _acess.GetProjects()
                           select project;
            return projects;


        }

1 answer

1


You’re trying to return a Type Projecting while its function returns String.

Instead of returning the Project, try returning (if any) its name for example or use the method ToString() if it makes sense.

public ActionResult<IEnumerable<string>> Get()
        {

            var projects = from project in _acess.GetProjects()
                           select project.Name;
            return projects;


        }

Or

public ActionResult<IEnumerable<string>> Get()
        {

            var projects = from project in _acess.GetProjects()
                           select project.ToString();
            return projects;


        }

you can also change the return type of your Controller:

public ActionResult<IEnumerable<SS_API.Model.Project>> Get()
        {

            var projects = from project in _acess.GetProjects()
                           select project;
            return projects;


        }
  • I appreciate your help. I was able to solve the problem by leaving the code as follows: public Actionresult<Ienumerable<Project>> Get() { var Projects = (from project in _acess.Getprojects() project select). Tolist(); Return Projects; }

  • Cool. This is the same thing suggested in my third option. Note that as the return is Ienumerable type you do not need to use Tolist(). You probably don’t need to use this one either from. I just hadn’t suggested it before because it wasn’t essential to correct the mistake, which is the main subject. Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.