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

JavaScript: splitting string from the first comma

javascript regex split comma

I have multiple lines of text in log files in this kind of format:

Difference between pic Vs pie

c++ c

I am looking the options for Binary Encryption Code generation and found two options available that are PIC and PIE. I have tried for find the differences between both but didn't find suitable arti...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.