views:

466

answers:

4

Hi,

A little newbie question. I have a foreach loop where I am trying to access the property of a row object and assign to a string.

foreach(row in Dataset)
{
 string finalName= row.name;

}

On each iteration I need to concat each row's name property to the finalName

\Thanks alot,

+10  A: 

Depending on your performance requirements you can either concat using the addition operator:

string finalName = string.Empty;
foreach(row in Dataset)
{
    finalName += row.name;    
}

or use a StringBuilder:

Stringbuilder sb = new StringBuilder();
foreach(row in Dataset)
{
    sb.Append(row.Name);   
}

string finalName = sb.ToString();

General for very small numbers of appends you won't notice a difference between the two versions. But if you are combining a lot of strings then there may be a very noticeable performance and memory benefit to using the StringBuilder.

Also bear in mind that this will place the strings directly one after the other so you may also wish to append a space or new line between each of them.


Special bonus LINQ one-liner:

Since you seem to be new to C# I'd suggest that you ignore this one since it is quite advanced and frankly not as readable (but on the other hand who doesn't like throwing random bits of LINQ into their code?). Still, I'll add it here for completeness. I don't know what the performance is like, but one major advantage over the other methods is the ease with with you can place a space or other character between the strings. Just change the value in the marked line to whatever value you want to separate the combined strings with (Environment.NewLine or " ", for example)

string finalName = string.Join(
                "", //Change the value here to separate the strings.
                (from row in Dataset
                 select row.Name).ToArray());

or, if you prefer lambdas to LINQ:

string finalName = string.Join(
            "", //Change the value here to separate the strings.
            Dataset.Select(row => row.Name).ToArray());
Martin Harris
I'm pretty sure you don't want "finalName" declared within the foreach.
Austin Salonen
You're right. Fixed.
Martin Harris
@Chris: He's concatenating data across ROWS, not COLUMNS. Thousands of records in a table is commonplace.
Adam Robinson
@AdamRobinson: oops my bad, I read the question wrong. Yeah, you'll want to use a StringBuilder!
Chris Pietschmann
+1  A: 

If you really just want to concat, then this is all you need...

string finalName = "";

foreach(DataRow row in dataset.Tables[tablename].Rows)
{
 finalName += row.name;

}
curtisk
+1  A: 

Here's one...

string finalName = ""

foreach (DataRow row in dataSet.TableX)
{
   finalName += row.Name;
   finalName += Environment.NewLine;
}

Here's another...

StringBuilder finalName = new StringBuilder()

foreach (DataRow row in dataSet.TableX)
{
    finalName.Append(row.Name);
    finalName.Append(Environment.NewLine);
}
Austin Salonen
+3  A: 

Just a couple of things...

  1. DataSet's don't have rows, they have DataTables, and those DataTables have DataRows
  2. You can use the += operator to perform a concatenation, though the general advice is that a StringBuilder object is better suited to this for performance and memory reasons.

Assuming your DataSet has one DataTable and the column you're after is called name, you can use either of these (the first using the += operator, the second using a StringBuilder)

string finalName = string.Empty;

foreach(DataRow row in dataSet.Tables[0].Rows)
{
    finalName += row["name"].ToString();
}

or

System.Text.StringBuilder builder = new System.Text.StringBuilder();

foreach(DataRow row in dataSet.Tables[0].Rows)
{
    builder.Append(row["name"].ToString());
}

string finalName = builder.ToString();
Adam Robinson