把一个表的DataRow[]转换为另外的datatable(小技巧)

        DataTable dt1 = new DataTable();
        dt1.Columns.Add("a");
        for (int i = 0; i < 10; i++)
        {
            DataRow dr1 = dt1.NewRow();
            dr1["a"] = "1";
            dt1.Rows.Add(dr1);
        }

        DataRow[] drw = dt1.Select("a =" + "1");
        DataTable dt2 = new DataTable();
        dt2 = dt1.Clone();
        foreach (DataRow Drr in drw)
        {
            DataRow dr2 = dt2.NewRow();//如果在此直接用dt2.Rows.Add(Drr)会报错,因为Drr的引用现在为dt1所用
            dr2.ItemArray = Drr.ItemArray;
            dt2.Rows.Add(dr2);
        }

你可能感兴趣的:(Datatable)