Outputting generic list to array

G

Guest

I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employee> emp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and organization
properties.

Any help is appreciated!
 
C

ClayB

Try this code.

Employee[] empArray = emp.ToArray();

Since emp is a List<Employee>, emp.ToArray returns an array of
Employees, not strings.

================
Clay Burch
Syncfusion, Inc
 
J

Jon Skeet [C# MVP]

MarkAurit said:
I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employee> emp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and organization
properties.

If you want to convert it to an array of strings, you'll have to
convert each Employee to a string individually, e.g. with ToString().
 
G

Guest

I can certainly do that. But then what is the purpose of ToArray()?

Jon Skeet said:
MarkAurit said:
I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employee> emp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and organization
properties.

If you want to convert it to an array of strings, you'll have to
convert each Employee to a string individually, e.g. with ToString().
 
B

Ben Voigt

SandpointGuy said:
I can certainly do that. But then what is the purpose of ToArray()?

converting from List<T> to T[].

Of course, you can do (not compile-tested):

Jon Skeet said:
MarkAurit said:
I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employee> emp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and
organization
properties.

If you want to convert it to an array of strings, you'll have to
convert each Employee to a string individually, e.g. with ToString().
 
S

stepf

yes, it's working! thanks

tested with:

string[] empArray = Array.ConvertAll<Employee, string>(emp.ToArray(), delegate(Employee from) { return from.ToString(); });

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top