1、封装独立方法
// 执行DataTable中的查询返回新的DataTable /// </summary> /// <param name="dt">源数据DataTable</param> /// <param name="condition">查询条件</param> /// <returns></returns> private DataTable GetNewDataTable(DataTable dt, string condition,string sortstr) { DataTable newdt = new DataTable(); newdt = dt.Clone(); DataRow[] dr = dt.Select(condition,sortstr); for (int i = 0; i < dr.Length; i++) { newdt.ImportRow((DataRow)dr[i]); } return newdt;//返回的查询结果 }
2、单独写出
DataRow[] drArr = ds.Tables[0].Select("ParentID=-1", "MenuOrder asc"); DataTable dtOneLevel = ds.Tables[0].Clone(); foreach (DataRow drOneLevel in drArr) { dtOneLevel.ImportRow(drOneLevel); }
3、转泛型筛选
var dtOneLevel = from dt in ds.Tables[0].AsEnumerable() where dt.Field<int>("ParentId") == -1 orderby dt.Field<int>("MenuOrder") ascending select dt;
原文:http://www.cnblogs.com/markli/p/4992561.html