执行DataTable中的查询返回新的DataTable

        ///
        /// 源数据DataTable
        /// 查询条件
        ///
        private DataTable GetNewDataTable(DataTable dt, string condition)
        {
            DataTable newdt = new DataTable();
            newdt = dt.Clone();//只复制表结构
            DataRow[] dr = dt.Select(condition);//查询出满足条件的dr
            for (int i = 0; i < dr.Length; i++)
            {
                newdt.ImportRow((DataRow)dr[i]);

               // 改为newdt.ImportRow(dr[i]);也行 ,没试过
            }
            return newdt;//返回的查询结果
        }

 

dt.Select()简单用法;

            Select("id>='3' and name='3--hello'");//支持and

            Select("id>='3' or id='1'");//支持or

            Select("name like '%hello%'");//支持like   

            Select("id>5","id desc");

            Select("id>5", "id desc",DataViewRowState.Added)

DataRow[] dr = dt.Select(“col='XXXX'”);//条件:就是字段名='某某'

你可能感兴趣的:(.net)