How to convert list object

Posted by Jayakumars under ADO.NET on 6/9/2018 | Points: 10 | Views : 927 | Status : [Member] [MVP] | Replies : 2
Hi

how to convert list to string array c# like this

List <VegRName> lvegname = new List<VegRName>();
lvegname.Add(new VegRName() { VEGID=1, VEGNAME= "Artichoke" });
lvegname.Add(new VegRName() { VEGID = 2, VEGNAME = "Bean" });
lvegname.Add(new VegRName() { VEGID = 3, VEGNAME = "Cabbage" });
lvegname.Add(new VegRName() { VEGID = 4, VEGNAME = "Carrot" });

i need this output
//string[] vegetables = new string[] { "Artichoke", "Bean", "Cabbage", "Carrot" };

i tried like this but i canot done this
string[] vegetables = new string[] { lvegname.ToArray().ToString()};
any one guide me

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com



Responses

Posted by: Bees7000 on: 7/29/2018 [Member] Starter | Points: 25

Up
0
Down
string[] vegetables = new string[lvegname.Count];
for ( int i = 0 ; i < lvegname.Count ; i++ )
{
vegetables[ i ] = lvegname[ i ].VEGNAME;
}

foreach ( var item in vegetables )
{
Console.WriteLine(item);
}


Marwan Alshemy

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deplivars1994 on: 8/30/2018 [Member] Starter | Points: 25

Up
0
Down
When you're working with arrays (which is the only way to do it here), you have to implement cycles. You can't just put whole list in array in one iteration. Honestly, i understand that sometimes coding might get intense and frustrating. When this happens to me i try to refresh my mind by playing at https://casinoscroll.com/ruby-fortune/ . Really good experience that sets you in a right mood.

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response