Click here to Skip to main content
15,909,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to store a column all rows in a array without any loop in asp .net c#
Posted
Comments
OriginalGriff 31-May-12 3:03am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Prasad_Kulkarni 31-May-12 3:16am    
Please elaborate with some code snippet..

1 solution

If the requirement is to populate an array from the values of all rows of a DataTable corresponding to a particular Column without using a loop, then LINQ query can be used as follows:
C#
DataTable dataTable1 = new DataTable("DataTable1");
dataTable1.Columns.Add("Month", typeof(string),null);
dataTable1.Columns.Add("Name", typeof(string),null);
dataTable1.Columns.Add("Amout", typeof(int),null);

dataTable1.Rows.Add("March","John",0);
dataTable1.Rows.Add("April","Ishan",25);
dataTable1.Rows.Add("May","Raman",50);
dataTable1.Rows.Add("March","Sheron",0);
dataTable1.Rows.Add("March","Ramesh",75);

string[] names = dataTable1.AsEnumerable().Select (t => t.Field<string>("Name")).ToArray();

//Contents of names
//John 
//Ishan 
//Raman 
//Sheron 
//Ramesh</string>
 
Share this answer
 
Comments
codeBegin 31-May-12 3:45am    
My 5..
VJ Reddy 31-May-12 4:02am    
Thank you, spk89 :)
Maciej Los 31-May-12 3:48am    
LINQ, of course ;)
+5!
VJ Reddy 31-May-12 4:02am    
Thank you, losmac :)

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