日期:2014-05-17 浏览次数:20477 次
protected void Page_Load(object sender, EventArgs e) { DataTable dt = "select id,name,Fid,LV from APP_Func".FSelect().FWhere("id<50 and LV<=4"); DateTime ww = DateTime.Now; for (int i = 0; i < 10; i++) { RecursiveClass(dt, 0, "fid", dt.Clone()).FRepeater(this.Repeater1); } Label1.Text = "循环【10】次的时间 " + ww.FTDiff(DateTime.Now).ToString() + " 秒,总共" + dt.Rows.Count + "条数据"; } /// <summary> /// [递归]无限级分类 /// </summary> /// <param name="ddtt">有数据的datatable</param> /// <param name="id">起初的ID</param> /// <param name="Fid">id=父类字段名</param> /// <param name="dt">返回的DT数据</param> /// <returns></returns> public DataTable RecursiveClass(DataTable data, int id, string Fid, DataTable dt) { for (int i = 0; i < data.Rows.Count; i++) { if (!(Select(dt, "id=" + id.ToString(), "").Rows.Count > 0) && id != 0) { dt = Select(data, "id=" + id.ToString(), ""); } if (int.Parse(data.Rows[i][Fid].ToString().Trim()) == id) { DataRow dr = dt.NewRow(); string[] sd = new string[dt.Columns.Count]; for (int j = 0; j < dt.Columns.Count; j++) { sd[j] = data.Rows[i][dt.Columns[j].ToString()].ToString(); } dr.ItemArray = sd; dt.Rows.Add(dr); RecursiveClass(data, int.Parse(data.Rows[i]["id"].ToString()), Fid, dt); } } return dt; } /// <summary> /// 执行DataTable中的查询返回新的DataTable /// </summary> /// <param name="dt">源数据DataTable</param> /// <param name="Where">查询条件【不需要加where】</param> /// <param name="Order">排序【不需要加Order by】</param> /// <returns>返回查询OK的DataTable</returns> public DataTable Select(DataTable dt, string Where, string Order) { DataTable newdt = new DataTable(); newdt = dt.Clone(); DataRow[] dr = dt.Select(Where, Order); for (int i = 0; i < dr.Length; i++) { newdt.ImportRow((DataRow)dr[i]); } return newdt; }