本帖最后由 Y2012 于 2013-11-15 17:23:55 编辑

解决方案 »

  1.   

    就是DataTable与List的数据相互转换
      

  2.   

    http://blog.sina.com.cn/s/blog_4938def90100w393.html
      

  3.   

    http://blog.csdn.net/zx13525079024/article/details/4962738
      

  4.   

    private static DataTable ConvertToDataTable<T>(T list) where T : IList//泛型参数类型限定T为IList类型
            {
                var table = new DataTable(typeof(T).GetGenericArguments()[0].Name);//通过反射获得DataTable的Name
                typeof(T).GetGenericArguments()[0].GetProperties().
                    ToList().ForEach(p => table.Columns.Add(p.Name, p.PropertyType));//通过反射获得属性名并且循环的初始化DataTable(列名,属性类型)
                foreach (var item in list)
                {
                    var row = table.NewRow();
                    item.GetType().GetProperties().
                        ToList().ForEach(p => row[p.Name] = p.GetValue(item, null));//lambda写法,将属性值循环的加入到DataTable的Row的每一列当中
                    table.Rows.Add(row);//加入到这个DataTable中
                }
                return table;//返回转换好的DataTable
            }