list集合转DataSet

  public static DataSet ListToDataSet<T>(List<T> list)
        {
            if (list.Count == 0) return new DataSet();
            var properties = list[0].GetType().GetProperties();
            var cols = properties.Select(p => new DataColumn(p.Name));
            var dt = new DataTable();
            dt.Columns.AddRange(cols.ToArray());
            list.ForEach(x => dt.Rows.Add(properties.Select(p => p.GetValue(x)).ToArray()));
            return new DataSet { Tables = { dt } };
        }

 

posted @ 2021-05-21 18:06  Ly博客  阅读(209)  评论(0编辑  收藏  举报