日期:2014-05-20 浏览次数:20861 次
class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("A"); dt.Columns.Add("B"); dt.Columns.Add("C"); dt.Columns.Add("D"); dt.Columns.Add("E"); for (int i = 0; i < 10; i++) { DataRow row = dt.NewRow(); row["A"] = i; row["B"] = i; row["C"] = i; row["D"] = i; row["E"] = i; dt.Rows.Add(row); } string[] columns = new string[] { "A", "B", "C" }; DataTable ret = ShowTable(dt, columns); } static DataTable ShowTable(DataTable dt, string[] columns) { var r = (from o in dt.Columns.Cast<DataColumn>() where !columns.Contains(o.ColumnName) select o.ColumnName).ToList().Distinct(); foreach (var colname in r) { dt.Columns.Remove(colname); } return dt; } }
------解决方案--------------------
private DataTable ShowTable(DataTable dt, string[] columns) { var targetDt = dt.Copy(); foreach(var col in targetDt .Columns) { if (!columns.Contains(col.ColumnName)) targetDt .Columns.Remove(col); } return targetDt; }
------解决方案--------------------
http://blog.csdn.net/q107770540/article/details/6556210
看看这个
------解决方案--------------------
这样做不是很浪费,把不要的子段查出来,然后有删除掉..........为何不找一个只要查询自己需要的字段呢
Linq实现这种随时变化的好像有点麻烦: http://www.4ucode.com/Study/Topic/1600