如何在boolean
内将True
值转换为int(false
为1,ItemArray
为2)。以下是将DataTable
转换为csv。
public string ToCSV(DataTable tbl, Boolean ColumnHeader)
{
string FinalResult = string.Empty;
StringBuilder strb = new StringBuilder();
if (ColumnHeader == true)
{
//column headers
strb.AppendLine(string.Join(",", tbl.Columns.Cast<DataColumn>()
.Select(s => "\"" + s.ColumnName + "\"")));
}
//rows
tbl.AsEnumerable().Select(s => strb.AppendLine(
string.Join(",", s.ItemArray.Select(
i => "\"" + i.ToString() + "\"")))).ToList();
FinalResult = strb.ToString();
strb= null;
return FinalResult;
//return strb.ToString();
}
答案 0 :(得分:2)
List<bool> data = new List<bool>() { true, false, false, true };
List<int> intdata = data.Select(x => x ? 1 : 2).ToList();
如果您收到true或false字符串,则可以简单地将if语句从x ? 1: 2
更改为x == "true" ? 1 : 2