Click here to Skip to main content
14,734,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am unable to separate array item by comma.

What I have tried:

List<string> list = new List<string>();


          foreach (DataRow row in dt.Rows)
          {

              list.Add((string)Convert.ToString(row["partDesc"]));
                }

          Array test = list.ToArray();
Posted
Updated 1-Apr-18 21:00pm

1 solution

Only strings can contain items separated by commas, and every item in the sting must also be a string - lists contain the items in their "native" format.
However it's not difficult at all to create a string from a List using string.Join:
List<int> myList = new List<int>() { 1, 2, 3, 4, 666, 17, 42 };
string commaSeparated = string.Join(",", myList);
This will use the default ToString implementation so it will work with any class that overrides ToString.

Alternatively, in your specific case, just use a StringBuilder:
StringBuilder sb = new StringBuilder();
string sep = "";

foreach (DataRow row in dt.Rows)
    {
    sb.AppendFormat("{0}{1}", sep, (string)Convert.ToString(row["partDesc"]));
    sep = ",";
    }
string commaSeparated = sb.ToString();
   
v2
Comments
ADI@345 2-Apr-18 2:06am
   
Thanks sir
OriginalGriff 2-Apr-18 5:52am
   
You're welcome!

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