cannot convert method group or convert array to list as its a method

I get an error in the bellow code it says I cannot convert method group ListStudents to generic.list, if I try () at the end of ListStudents I get: cant convert array to list

All im trying to do is hardcode some users to an array and return them in my GET request is there a fix for this?

    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "")]
    List<Student> GetStudentCollection();
}
[DataContract(Name="Student")]
public class Student
{
    [DataMember(Name = "StudentID")]
    public string StudentID { get; set; }
    [DataMember(Name = "FirstName")]
    public string FirstName { get; set; }
    [DataMember(Name = "LastName")]
    public string LastName { get; set; }

}
public class RawDataService : IReceiveData
{
    public Student[] ListStudents()
    {
       List<Student> student = new List<Student>()
       {
           new Student { StudentID="bla", FirstName="bla", LastName="bla"},
           new Student { StudentID="bla1", FirstName="bla1", LastName="bla1"},
           new Student { StudentID="bla2", FirstName="bla2", LastName="bla2"}
       };
        return student.ToArray();
    }

    public List<Student> GetStudentCollection()
    {
        return ListStudents(); //error on this line, cant convert array to list
    }

Answers


Try using the ToList linq extension method:

using System.Linq;

...

public Student[] ListStudents()
{
   List<Student> student = new List<Student>()
   {
       new Student { StudentID="bla", FirstName="bla", LastName="bla"},
       new Student { StudentID="bla1", FirstName="bla1", LastName="bla1"},
       new Student { StudentID="bla2", FirstName="bla2", LastName="bla2"}
   };
    return student.ToArray();
}

public List<Student> GetStudentCollection()
{
    return ListStudents().ToList(); //<-- this is the change you need
}

However, to reduce the number of conversions I would suggest changing the methods as follows:

public Student[] ListStudents()
{
   return GetStudentCollection().ToArray();
}

public IList<Student> GetStudentCollection()
{
   var students = new List<Student>()
   {
       new Student { StudentID="bla", FirstName="bla", LastName="bla"},
       new Student { StudentID="bla1", FirstName="bla1", LastName="bla1"},
       new Student { StudentID="bla2", FirstName="bla2", LastName="bla2"}
   };

   return students();
}

ListStudents is a method, not a property, so try this:

return ListStudents();

Also, change ListStudents() method to return a List like this:

    public List<Student> ListStudents()
    {
        List<Student> student = new List<Student>()
        {
            new Student { StudentID="bla", FirstName="bla", LastName="bla"},
            new Student { StudentID="bla1", FirstName="bla1", LastName="bla1"},
            new Student { StudentID="bla2", FirstName="bla2", LastName="bla2"}
        };
        return student;
    }

Unless you have a reason for using an array just return the list as is. Why spend the CPU to convert down to an array, then back up to a List:

public class RawDataService : IReceiveData
{
    public List<Student> ListStudents()
    {
       List<Student> students = new List<Student>()
       {
           new Student { StudentID="bla", FirstName="bla", LastName="bla"},
           new Student { StudentID="bla1", FirstName="bla1", LastName="bla1"},
           new Student { StudentID="bla2", FirstName="bla2", LastName="bla2"}
       };
        return students;
    }

    public List<Student> GetStudentCollection()
    {
        return ListStudents(); //error on this line, cant convert array to list
    }

But, if you must return the array, then change the line with the error to:

  using System.Linq;
  ...
  return ListStudents().ToList();

Need Your Help

How do I modify this to upload multiple files?

php arrays post file-upload upload

My upload code is this. I submit the image to it with postdata. I want to make the file select box accept multiple picutes, and have it work on the backend. I can get the header location to work just

Leiningen/Clojure is acting weird when I try to connect to Postgresql Database

sql postgresql clojure

I am trying to follow the instructions here: https://devcenter.heroku.com/articles/clojure-web-application