废话不多说了,直接上代码:

       public static void Main()
        {
            Run();
        }

        public static void Run()
        {
            List<DataRow> rows = new List<DataRow> { };
            rows.AddRange(CreateTable().Select("ID>1"));

            //以下是关键代码,IEnumerable<DataRow>的一扩展方法
            DataTable newTable = rows.CopyToDataTable(); 

            Console.WriteLine(newTable.Rows.Count);// return 3
        }

        public static DataTable CreateTable()
        {
            DataTable table = new DataTable();

            table.Columns.Add(new DataColumn("ID",typeof(int)));
            table.Columns.Add(new DataColumn("Name", typeof(string)));

            for (int i = 0; i < 5; i++)
            {
                DataRow row = table.NewRow();
                row[0] = i;
                row[1] = "name" + i;
                table.Rows.Add(row);
            }
            return table;
        }

CopyToDataTable方法是IEnumerable<DataRow>的一扩展方法,.NET Framework3.5以上都有这个方法,希望能够帮到大家,少写点代码。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2022-12-23
猜你喜欢
  • 2022-02-03
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2022-03-01
  • 2022-02-20
相关资源
相似解决方案