[英]How to add “String[] or List<String>” as ONE cell entry in a DataTable in C#?
I am trying to create a datatable containing cells with lines > 1. (FYI: new to C#, doesn't know anything about OOPS...Please be kind) This is my code: 我正在尝试创建一个数据表,其中包含行数大于1的单元格。(仅供参考:C#的新手,对OOPS一无所知...请亲切)这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;
namespace Excelapp
{
class Program
{
static void Main(string[] args)
{
DataTable table = new DataTable();
table.Columns.Add("LOCarray",typeof(String[]));
table.Columns.Add("LOCstring", typeof(String));
table.Columns.Add("LOClist", typeof(List<String>));
String[] LOCarray = new String[2] {"Line1","Line2" };
String LOCstring = "Line1\n\rLine2";
List<String> LOClist = new List<String>();
LOClist.Add("Line1");
LOClist.Add("Line2");
table.Rows.Add(LOCarray, LOCstring, LOClist);
}
}
}
Output is: 输出为:
As you can see, This is not what I want. 如您所见,这不是我想要的。 Even when I am writing as String it is not showing \\n character. 即使当我写为String时,它也不显示\\ n字符。
Please help me. 请帮我。
string[] arr = { "one", "two", "three" };
string arrayValuesWithNewLineSep = string.Join("\n", arr));
Another solution if you prefer Linq is to use the Aggregate
function. 如果您更喜欢Linq,另一种解决方案是使用Aggregate
函数。
Example: 例:
IEnumerable<string> collection = new List<string>() { "This", "is", "a", "sentence", "."};
var sentence = collection.Aggregate((a, b) => a + " " + b);
Or in your case, it'd be 或者在您的情况下
var myValue = locList.Aggregate((a, b) => a + "\n\r" + b);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.