我有 2 个数据表,dt_columns
和dt_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);