/// <summary> /// Converts generic object to DataTable with 1 row /// </summary> /// <param name="x"></param> /// <returns></returns> private DataTable GetDataTable(object x) { var m = new XMap(x); var t = new DataTable(); var r = new object[m.Values.Count]; var i = 0; Type type; Type nnType; m.ForEach(d => { type = m.Types[d.Key]; nnType = Nullable.GetUnderlyingType(type); if (nnType != null) type = nnType; t.Columns.Add(d.Key, type); if (d.Value == null) r[i++] = DBNull.Value; else r[i++] = (m.Types[d.Key] == typeof(DateTime) && (DateTime)d.Value == DateTime.MinValue) ? DBNull.Value : d.Value; }); t.Rows.Add(r); return t; }
/// <summary> /// Converts generic list of objects to DataTable /// </summary> /// <param name="x"></param> /// <returns></returns> private DataTable GetDataTable(IList x) { if (x.Count < 1) return null; var m = new XMap(x[0]); var c = m.Values.Count; var t = GetDataTable(x[0]); object[] r; var rt = x.Count; for (int i = 0, rc = 1; rc < rt; i = 0, rc++) { r = new object[c]; m = new XMap(x[rc]); m.ForEach(d => { if (d.Value == null) r[i++] = DBNull.Value; else r[i++] = (m.Types[d.Key] == typeof(DateTime) && (DateTime)d.Value == DateTime.MinValue) ? DBNull.Value : d.Value; }); t.Rows.Add(r); } return t; }