DataTable tblData = new DataTable();
MySQLProcessor.dtTable(pullDataQuery, out tblData);
foreach (DataRow columnRow in tblData.Rows)
{
//do string work here
}
columnRow看起来像这样
[0]苹果
[1]梨
[2]芒果
我想把它变成一个看起来像Apple | Pear | Mango
的字符串
没有在数组中执行foreach循环。
Soryr没有说清楚,每张桌子都有不同数量的阵列。
答案 0 :(得分:4)
尝试
object[] arr = new object[] {"1", "2" };
string joinedStr = string.Join("|",arr.Select(p => p.ToString()).ToArray());
因此,您的代码可能会变成
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
}
答案 1 :(得分:2)
考虑String.Join
。必须先提取有问题的列值,也许......
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);
......或类似的。
快乐的编码。
答案 2 :(得分:0)
迈克,你可以做这样的事情
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
}
答案 3 :(得分:0)
//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, ",") ;
}
答案 4 :(得分:-2)
试试这个:
string record = columnRow[0].ToString() + "|" + columnRow[1].ToString() + "|" + columnRow[2].ToString();