简体   繁体   中英

How to concatenate multiple column values into one column in datatable c#

I have a datatable with 3 columns( col1,col2,col3) I have added an extra column with col4(Lets say).

Now My requirement is :

Col1 ||    col2 ||  col3  ||  col4

A          B        C         Col1-A;Col2-B;Col3-C 

Basically, I want a concatenated values in the new column of the existing column name and values as demonstrated above. Hope my requirement is understood. Thanks in Advance!

This might do the trick for you

DataColumn newColumn;
newColumn = new DataColumn("col4");
newColumn.Expression =  string.Format("Col1\-{0};Col2\-{1};Col3\-{2}", col1, col2, col3);
scaleResponseData.Columns.Add(newColumn);

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