I need to return an IEnumerable.
Here is the working code:
public IEnumerable<String> ReturnStudentsAsString(int vak, int klasgroep)
{
return (from s in Students
orderby s.Naam, s.Voornaam
select s.Naam);
}
That works perfect!
Now, I want to return the street and the postal code as well…
So I created an anonymous type:
public IEnumerable<String> ReturnStudentsAsString(int vak, int klasgroep)
{
var test = (from s in Studenten
orderby s.Naam, s.Voornaam
select new
{
StudentNumber = s.StudentNumber,
Name = s.Name,
Street = s.Street,
PostalCode = s.PostalCode,
City = s.City
});
return test;
But it gives me an error…
How could I fix it?
Thanks in advance!
It is no longer an
IEnumerable<String>. You’d have to return anIEnumerable<object>to pass the anonymous type back from your query.Edit: As others have suggested, a better approach would probably be to define a static type to hold the values you want to return. Alternately, since this is basically a one-liner query, you could do it in-line wherever you’re using this method instead of creating a specialized method for it.