C# DataTable某一列转换为Array数组或List<>集合
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace ConsoleApp26
{
class Program
{
// 添加引用:System.Data.DataSetExtensions
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("title", typeof(string));
dt.Columns["id"].AutoIncrement = true;
for (int i = 0; i < 10; ++i)
{
DataRow newRow = dt.NewRow();
newRow["title"] = "标题" + i;
dt.Rows.Add(newRow);
}
// System.Data.DataSetExtensions
string[] array = dt.AsEnumerable().Select(d => d.Field<string>("title")).ToArray();
List<string> list = dt.AsEnumerable().Select(d => d.Field<string>("title")).ToList();
Console.Read();
}
}
}