Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
List<student> studIDList = handler.GetStudentIDList();
int[] studID = studIDList.ToArray();


This is the code i have but it does not work.
Assume studIDList have been populated.

Thank you in advance.
http://www.codeproject.com/script/Membership/View.aspx?mid=8136054[^]
Posted
Updated 18-Aug-14 2:36am
v2

You can't convert a List of student class instances to integers - the system doesn't know how to do that.

But, if the student class has an integer property such as an ID number you can get an array of those:
C#
List<student> studIDList = handler.GetStudentIDList();
int[] studID = studIDList.Select(s => s.Id).ToArray();


[edit]Typos - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Rudolfskow 19-Aug-14 2:39am    
I know i can convert a list of students class instance to integers...But i ran out of options cause nothing i tried seem to work...and that why I came here...And Solution 2 work for me...Thank you btw...
1.The method ToArray() returns an array of Student objects;

2.If you need an array of integer with the ID of the students, you have to iterate your first list and create a list of IDs, then to use ToArray() on this 2nd list, like in example bellow:
C#
List<student> studIDList = handler.GetStudentIDList();
List<int> idList= new List<int>;
foreach(student item in studIDList)
{
  idList.Add(item.ID); //Here you should use the property used for student ID!
}
//
int[] studID = idList.ToArray();
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 12:11pm    
5ed, but this loop is already implemented in generic List; so the solution is just one like of code; please see my Solution 3.
—SA
Rudolfskow 19-Aug-14 2:41am    
This work Perfectly...I was sitting with the problem for so long...Thank you so much...now I can continue with my other problems... :) ...Thanx again I really Appreciate it...
Raul Iloc 19-Aug-14 2:48am    
I am glad that I could help you! If my solution works for you, it will be nice from you to accept it.
In addition to Solution 2:
The iteration is made in one line using the method already implemented in the class System.Collections.Generic.List<>:
http://msdn.microsoft.com/en-us/library/73fe8cwf%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
Raul Iloc 19-Aug-14 1:01am    
Yes, this solution is better then mine. You have my vote!
Sergey Alexandrovich Kryukov 19-Aug-14 1:15am    
I appreciate that, Raul.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900