Common Operations of DataTable in C,

Source: Internet
Author: User

Common Operations of DataTable in C,

1. Convert the generic collection class to a able (used when there is no data in the table ):

  public static DataTable NullListToDataTable(IList list)        {            var result = new DataTable();            if (list.Count <= 0) return result;            var propertys = list[0].GetType().GetProperties();            foreach (var pi in propertys)            {                if (pi != null)                {                    result.Columns.Add(pi.Name, pi.PropertyType);                }            }            for (var i = 0; i < list.Count; i++)            {                var tempList = new ArrayList();                foreach (var pi in propertys)                {                    var obj = pi.GetValue(list[i], null);                    tempList.Add(obj);                }                var array = tempList.ToArray();                result.LoadDataRow(array, true);            }            return result;        }

2. Convert the generic collection class to a able (used when the table contains data ):

 public static DataTable NoNullListToDataTable<T>(IList<T> list)        {            var ds = new DataSet();            var dt = new DataTable(typeof(T).Name);            var myPropertyInfo =                typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);            foreach (var t in list)            {                if (t == null) continue;                var row = dt.NewRow();                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)                {                    var pi = myPropertyInfo[i];                    var name = pi.Name;                    if (dt.Columns[name] != null) continue;                    DataColumn column;                    if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]")                    {                        column = new DataColumn(name, typeof(int));                        dt.Columns.Add(column);                        if (pi.GetValue(t, null) != null)                            row[name] = pi.GetValue(t, null);                        else                            row[name] = DBNull.Value;                    }                    else                    {                        column = new DataColumn(name, pi.PropertyType);                        dt.Columns.Add(column);                        row[name] = pi.GetValue(t, null);                    }                }                dt.Rows.Add(row);            }            ds.Tables.Add(dt);            return ds.Tables[0];        }

3. If the table contains data or no data, you can exclude that DATASET does not support System. Nullable errors:

 public static DataTable ToDataTable<T>(IList<T> list)        {            if (list == null || list.Count <= 0)            {                var result = new DataTable();                object temp;                if (list == null || list.Count <= 0) return result;                var propertys = list[0].GetType().GetProperties();                foreach (var pi in propertys)                {                    {                        result.Columns.Add(pi.Name, pi.PropertyType);                    }                }                for (var i = 0; i < list.Count; i++)                {                    var tempList = new ArrayList();                    foreach (var pi in propertys)                    {                        var obj = pi.GetValue(list[i], null);                        tempList.Add(obj);                    }                    var array = tempList.ToArray();                    result.LoadDataRow(array, true);                }                return result;            }            var ds = new DataSet();            var dt = new DataTable(typeof(T).Name);            var myPropertyInfo =                typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);            foreach (var t in list)            {                if (t == null) continue;                var row = dt.NewRow();                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)                {                    var pi = myPropertyInfo[i];                    var name = pi.Name;                    if (dt.Columns[name] != null) continue;                    DataColumn column;                    if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]")                    {                        column = new DataColumn(name, typeof(int));                        dt.Columns.Add(column);                        if (pi.GetValue(t, null) != null)                            row[name] = pi.GetValue(t, null);                        else                            row[name] = DBNull.Value;                    }                    else                    {                        column = new DataColumn(name, pi.PropertyType);                        dt.Columns.Add(column);                        row[name] = pi.GetValue(t, null);                    }                }                dt.Rows.Add(row);            }            ds.Tables.Add(dt);            return ds.Tables[0];        }

4. Merge the same able:

   public static DataTable MergeSameDatatable(DataTable dataTable1, DataTable dataTable2)        {            var newDataTable = dataTable1.Clone();            var obj = new object[newDataTable.Columns.Count];            for (var i = 0; i < dataTable1.Rows.Count; i++)            {                dataTable1.Rows[i].ItemArray.CopyTo(obj, 0);                newDataTable.Rows.Add(obj);            }            for (var i = 0; i < dataTable2.Rows.Count; i++)            {                dataTable2.Rows[i].ItemArray.CopyTo(obj, 0);                newDataTable.Rows.Add(obj);            }            return new DataTable();        }

5. merge two different able columns into a new DataTable:

 public static DataTable UniteDataTable(DataTable dt1, DataTable dt2, string dtName)        {            var dt3 = dt1.Clone();            for (var i = 0; i < dt2.Columns.Count; i++)            {                dt3.Columns.Add(dt2.Columns[i].ColumnName);            }            var obj = new object[dt3.Columns.Count];            for (int i = 0; i < dt1.Rows.Count; i++)            {                dt1.Rows[i].ItemArray.CopyTo(obj, 0);                dt3.Rows.Add(obj);            }            if (dt1.Rows.Count >= dt2.Rows.Count)            {                for (var i = 0; i < dt2.Rows.Count; i++)                {                    for (var j = 0; j < dt2.Columns.Count; j++)                    {                        dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();                    }                }            }            else            {                for (var i = 0; i < dt2.Rows.Count - dt1.Rows.Count; i++)                {                    var dr3 = dt3.NewRow();                    dt3.Rows.Add(dr3);                }                for (var i = 0; i < dt2.Rows.Count; i++)                {                    for (var j = 0; j < dt2.Columns.Count; j++)                    {                        dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();                    }                }            }            dt3.TableName = dtName;            return dt3;        }

6. Datatable to List <Dictionary <string, object>:

 public static List<Dictionary<string, object>> DataTableToListDictory(DataTable table)        {            var ld = new List<Dictionary<string, object>>();            for (var i = 0; i < table.Rows.Count; i++)            {                var dic = new Dictionary<string, object>();                for (var j = 0; j < table.Columns.Count; j++)                {                    dic.Add(table.Columns[j].ColumnName, table.Rows[i][j]);                }                ld.Add(dic);            }            return ld;        }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.