开发者

C# DataRow all itemarrays to single string, appending | to each array

开发者 https://www.devze.com 2023-03-18 07:54 出处:网络
DataTable tblData = new DataTable(); MySQLProcessor.dtTable(pullDataQuery, out tblData); foreach (DataRow columnRow in tblData.Rows)
 DataTable tblData = new DataTable();
                MySQLProcessor.dtTable(pullDataQuery, out tblData);
                foreach (DataRow columnRow in tblData.Rows)
                {
                  //do string work her开发者_开发知识库e
                }

columnRow looks like this

[0]Apple

[1]Pear

[2]Mango

I want to turn it into a string that looks like Apple|Pear|Mango

without doing a foreach loop in the array.

Soryr for not making it clear, each table comes out with a different amount of arrayitems.


Try doing

object[] arr = new object[] {"1", "2" };
string joinedStr = string.Join("|",arr.Select(p => p.ToString()).ToArray());

So, your code could become

     DataTable tblData = new DataTable();
              string myStr = string.Empty; 
                    MySQLProcessor.dtTable(pullDataQuery, out tblData);
                    foreach (DataRow columnRow in tblData.Rows)
                    {
                      myStr = string.Join("|",columnRow.ItemArray.Select(p => p.ToString()).ToArray());
                      //do whatever you want
                    }


Consider String.Join. The columns values in question must be extracted first, perhaps...

var cols = row.ItemArray
    .Select(i => "" + i) // Not i.ToString() so when i is null -> ""
    .ToArray(); // For .NET35 and before, .NET4 Join takes IEnumerable

var res = string.Join("|", cols);

...or similar.

Happy coding.


Mike you can do something like this

string finalString=string.Empty;

foreach (DataRow columnRow in tblData.Rows)
                {
           finalString+=columnRow["ColumnName"]+"|";
                }
if(finalyString.length>0)
{
finalyString=finalString.Substring(0,finalString.length-1) // this removes extra "|" at the end
}


//Without Using LINQ. In case you use old DotNet (Eg.2.0)
string ArrToStr(DataRow r,string separator)
{
   string temp = "";
   Object[] o = r.ItemArray;
   foreach(Object oo in o)
   {
     temp += oo.ToString() + separator;
   }            
   return temp;
}
//So you can use like this.
string value="";
foreach (DataRow row in combine.Rows)
{
   value += ArrToStr(row, ",") ;
}


[Edited]:

You can get all the values of DataRow by it's property ItemArray. ItemArray is array of values which data types are types of object.

You can pass this array to function string.Join() which concatenates all the elements of array, using the specified separator between each element.

Be carefull because if your DataRow contains DBNull.Value in any of the columns, function string.Join() will implicity convert DBNull.Value into empty string (""). So in the end you can get something like this A||B. To avoid this situation I used in my example LINQ function Where() to get rid of empty values of DataRow and turn it into array again by using LINQ function ToArray(). More about DBNull.Value.ToString() you can find here msdn source .

In foreach loop you can notice that I have used dollar sign - $ (special character which identifies interpolated string), which is used for string interpolation.

"String interpolation is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values"

In this case placeholder is enclosed in a pair of curly brackets {}. By using string interpolation I got rid of another + sign operator for concatenating string literal "\r\n".

string str = "";
    
foreach (DataRow columnRow in tblData.Rows)
{
    str += $"{string.Join("|", columnRow.ItemArray.Where(val => val != DBNull.Value).ToArray())}\r\n";
}


Try this:

string record = columnRow[0].ToString() + "|" + columnRow[1].ToString() + "|" + columnRow[2].ToString();
0

精彩评论

暂无评论...
验证码 换一张
取 消