日期:2014-05-18 浏览次数:20444 次
#region Join /// <summary> /// 连接表 /// </summary> /// <param name="dt">将要被连接的表</param> /// <param name="col">主键字段</param> /// <param name="dt2">带有主键的表</param> /// <param name="insertAt">插入到的位置,-1不需要插入</param> /// <param name="cols">连接的字段</param> public static DataTable Join(this DataTable dt, string col, DataTable dt2, int insertAt, params string[] cols) { foreach (var it in cols) { var c = dt.Columns.Add(it, dt2.Columns[it].DataType); if (insertAt >= 0) c.SetOrdinal(insertAt++); } foreach (DataRow row in dt.Rows) { var row2 = dt2.Find(row[col]); if (row2 == null) continue; cols.For(it => row[it] = row2[it]); } return dt; } #endregion