.NET 使用动态列设置获取数据表行



我有 2 个数据表,dt_columnsdt_rows;dt_columns有一个动态列dt_rows。如何在dt_columns中使用IEnumerable<string>dt_rows中检索/选择数据、字段并将其导出到文本文件中?

dt_columns数据表:

header_name custom_header_name
列 1 custom_header_1
列2 custom_header_2

我认为这样的事情应该有效:

string[] colNames = dt_headers.AsEnumerable().Select(r => r.Field<string>("header_name")).ToArray();
string[] custColNames = dt_headers.AsEnumerable().Select(r => r.Field<string>("custom_header_name")).ToArray();
// build row values separated by the delimiter as strings
IEnumerable<string> data = dt_rows.AsEnumerable()
.Select(r => string.Join(delimiter, colNames.Select(c => r.Field<string>(c))));
// prepend header(custom column names):
data = data.Prepend(string.Join(delimiter, custColNames));
// write to text-file
File.WriteAllLines(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "\" + "resources\", file_name + ".txt"),
data);

最新更新