0

我在下面的代码中收到一个错误,它说我无法将方法组 ListStudents 转换为 generic.list,如果我在 ListStudents 末尾尝试 () 我得到:无法将数组转换为列表

我想做的就是将一些用户硬编码到一个数组中并在我的 GET 请求中返回它们是否有解决方法?

    [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
    }
4

3 回答 3

2

ListStudents是一个方法,而不是一个属性,所以试试这个:

return ListStudents();

此外,更改ListStudents()方法以返回List如下:

    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;
    }
于 2012-04-05T17:29:03.780 回答
1

尝试使用ToListlinq 扩展方法:

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
}

但是,为了减少转换次数,我建议更改方法如下:

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();
}
于 2012-04-05T17:30:58.300 回答
1

除非您有使用数组的理由,否则只需按原样返回列表。为什么要花 CPU 向下转换为数组,然后再备份为 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
    }

但是,如果您必须返回数组,则将出现错误的行更改为:

  using System.Linq;
  ...
  return ListStudents().ToList();
于 2012-04-05T17:35:33.793 回答