![](/img/trans.png)
[英]How to add a Dictionary<string,List<string>> to a DataTable in C#?
[英]How to add “String[] or List<String>” as ONE cell entry in a DataTable in C#?
我正在嘗試創建一個數據表,其中包含行數大於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);
}
}
}
輸出為:
如您所見,這不是我想要的。 即使當我寫為String時,它也不顯示\\ n字符。
請幫我。
string[] arr = { "one", "two", "three" };
string arrayValuesWithNewLineSep = string.Join("\n", arr));
如果您更喜歡Linq,另一種解決方案是使用Aggregate
函數。
例:
IEnumerable<string> collection = new List<string>() { "This", "is", "a", "sentence", "."};
var sentence = collection.Aggregate((a, b) => a + " " + b);
或者在您的情況下
var myValue = locList.Aggregate((a, b) => a + "\n\r" + b);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.