Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through a dataset

Tags:

c#

I'm looping through the rows and columns of a DataSet to write to a text file with a pipe delimiter. I have it working except I need to leave off the pipe from the last column. Is there a way for me to adjust the loop to loop through fields.count-1 and then refer to the last field outside of the loop?

foreach (object item in row.ItemArray)
{
    d = d+ item.ToString() + "|";
}
like image 382
Leehbi Avatar asked Jul 28 '26 12:07

Leehbi


2 Answers

You can join the contents of any IEnumerable (including arrays) using String.Join, eg:

var newText = String.Join("|",row.ItemArray);
like image 112
Panagiotis Kanavos Avatar answered Jul 30 '26 03:07

Panagiotis Kanavos


You can use TrimEnd(Char[]) method like

d.TrimEnd(new char[]{'|'});
like image 39
Rahul Avatar answered Jul 30 '26 02:07

Rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!