简体   繁体   中英

Better way to add comma separated value to a datatable column when adding a new row in datatable

I am adding new row in datatable in which i want to add comma separated value in 1 datatable column .

This is how i am doing:

var dt = new DataTable();

var data=Helper.GetData();

 foreach (DataRow dr in data)
 {
      DataRow dr = dt.NewRow();
      var append = new StringBuilder();
      var columns=Helper.GetColumns();

      char[] characters = new char[] { ' ', ',' };
      foreach(var item in columns)
      {
          if (columns.Count()==1)
          {
               dr["Id"] = dr[item].ToString();
               dr["Col1"] = dr[item].ToString();
          }
          else
          {
               dr["Id"] = dr[item].ToString();
               append.Append(dr[item].ToString() + ", ");
          }
      }
      if (columns.Count() > 1)
      {
          dr["Col1"] = append.ToString().TrimEnd(characters);//remove comma from the end
      }    
 } 

Is there any better way to do this???

Put your individual column values into a list and then concatenate them with String.Join :

var dt = new DataTable();

var data=Helper.GetData();

 foreach (DataRow dr in data)
 {
      List<string> values = new List<string>();
      DataRow dr = dt.NewRow();

      var columns=Helper.GetColumns();

      foreach(var item in columns)
      {
          dr["Id"] = dr[item].ToString();
          values.Add(dr[item].ToString());
      }
      dr["Col1"] = string.Join(",", values);
 } 

I admit to being a little confused about why you overwrite the "Id" column, but the code above should have the same effect as what you originally posted.

If I understood what you want right, here is a sample that depicts a simpler way:

DataTable dt = new DataTable("SO Example");

dt.Columns.Add("A", typeof(int));
dt.Columns.Add("B", typeof(string));
dt.Columns.Add("C", typeof(string));
dt.Columns.Add("D", typeof(string));
dt.Columns.Add("Concatenation", typeof(string));

dt.Rows.Add(1, "bbbbbb", "tra la la", "d");
dt.Rows.Add(2, "b b b", "tttt", "dddddd");
dt.Rows.Add(3, "b-b-b-b-b-b", "C", "d.d.d.d.d.d");
dt.Rows.Add(4, "bBbBbBb", "CCC", "dd");
dt.Rows.Add(5, "B", "C", "D");

foreach (DataRow row in dt.Rows)
{
    row["Concatenation"] = string.Join(", ", row.ItemArray.Take(row.ItemArray.Length - 1));
}

The table:

在此处输入图片说明

Output:

在此处输入图片说明

I think that Veverke answer indicates the absolute best solution for you. I just right tried to assemble your code integrating his solution...

var dt = new DataTable();
var data=Helper.GetData();
foreach (DataRow drSrc in data) //changed from "dr" in "drSrc" to readability (further, compiler should rises a syntax error)
{
   DataRow dr = dt.NewRow();

   dr["id"] = drSrc[drSrc.ItemArray.Length - 2]; //the last value of drSrc (after clarifications in comments and chat..)
   dr["Col1"]  = string.Join(", ", drSrc.ItemArray.Take (drSrc.ItemArray.Length - 1));

   dt.Rows.Add(dr);  //it missed in your code, but I suppose you want add it into the datatable 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM