Datatable C ා column names in DataRow in use are not case sensitive

Keywords: Attribute

We see a piece of code that needs to dynamically transform the model in our work. When there are many data, the efficiency will be slow. Though C ා is case sensitive, it is fuzzy in Datarow, so C ා is very useful. The example Demo is as follows, and the second method is recommended.
Recommended writing:

        public static List<T> DataTableToList2<T>(DataTable dt)
        {
            List<T> lists = new List<T>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T t = Activator.CreateInstance<T>();
                //Get all properties of the current T object
                PropertyInfo[] propertys = t.GetType().GetProperties();
                //Iterating out each attribute object through attribute set
                foreach (PropertyInfo item in propertys)
                {
                    int index = dt.Columns.IndexOf(item.Name);// Column names in datatable are not case sensitive,
                    if (index>=0)//dt.Columns.Contains(item.Name), it is faster to read the value with index
                    {
                        //Determine whether the single column value of the current DataTable is null
                        if (dt.Rows[i][index] != DBNull.Value)
                            //Assign the single column value of the current DataTable to the matching property, otherwise assign a null value
                            item.SetValue(t, (dt.Rows[i][index]), null);
                        else
                            item.SetValue(t, null, null);

                    }

                }
                lists.Add(t);
            }
            return lists;
        }

Don't bother to write separately, just write the code:

    public class TmpMode
    {
        public string AAA { get; set; }
        public string BBB { get; set; }
        public string c { get; set; }
        public string AAA1 { get; set; }
        public string BBB2 { get; set; }
        public string AAA3 { get; set; }
        public string BBB4 { get; set; }
        public string AAA5 { get; set; }
        public string BBB6 { get; set; }
        public string AAA7 { get; set; }
        public string BBB8 { get; set; }
        public string AAA9 { get; set; }
        public string BBB10 { get; set; }
        public string BBB11 { get; set; }
        public string AAA12 { get; set; }
        public string BBB12 { get; set; }
        public string AAA14 { get; set; }
        public string BBB15 { get; set; }
    }
    public class DataTableTest
    {
        List<TmpMode> lstModes = new List<TmpMode>();
        public void TestColumsNameIngore()
        {
            long first = 0, second = 0, three = 0;
            DataTable t = new DataTable("mytable");
            DataColumn col = new DataColumn("Aaa", System.Type.GetType("System.String"));
            t.Columns.Add(col);
            col = new DataColumn("Bbb", System.Type.GetType("System.String"));
            t.Columns.Add(col);
            col = new DataColumn("c", System.Type.GetType("System.String"));
            t.Columns.Add(col);
            col = new DataColumn("ddd", System.Type.GetType("System.String"));
            t.Columns.Add(col);
            for (int i = 0; i < 100; i++)
            {
                DataRow row = t.NewRow();
                row["Aaa"] = "aaa" + i;
                row["Bbb"] = "bbb" + i;
                row["c"] = "c" + i;
                t.Rows.Add(row);
            }
            StringBuilder str = new StringBuilder();
            Stopwatch watch = new Stopwatch();
            watch.Start();
            lstModes = DataTableToList<TmpMode>(t);


            foreach (var s in lstModes)
            {
                str.AppendLine(s.AAA + "  " + s.BBB + "  " + s.c);
            }
            watch.Stop();
            Console.WriteLine("The first conversion:");
            Console.WriteLine(str.ToString());
            Console.WriteLine("Time:" + watch.ElapsedMilliseconds);
            first = watch.ElapsedMilliseconds;

            lstModes.Clear();
            Console.WriteLine("Empty model:"+lstModes.Count);
            watch.Reset();
            watch.Start();
            lstModes = DataTableToList2<TmpMode>(t);
            str.Clear();
            foreach (var s in lstModes)
            {
                str.AppendLine(s.AAA + "  " + s.BBB + "  " + s.c);
            }
            watch.Stop();
            Console.WriteLine("The second conversion:");
            Console.WriteLine(str.ToString());
            Console.WriteLine("Time:" + watch.ElapsedMilliseconds);
            second = watch.ElapsedMilliseconds;

            lstModes.Clear();
            Console.WriteLine("Empty model:" + lstModes.Count);
            watch.Reset();
            watch.Start();
            lstModes = DataTableToList3<TmpMode>(t);
            str.Clear();
            foreach (var s in lstModes)
            {
                str.AppendLine(s.AAA + "  " + s.BBB + "  " + s.c);
            }
            watch.Stop();
            Console.WriteLine("The Third Transformation:");
            Console.WriteLine(str.ToString());
            Console.WriteLine("Time:" + watch.ElapsedMilliseconds);
            three = watch.ElapsedMilliseconds;

            Console.WriteLine("contrast:" + first +"   "+second+"  "+three);
            Console.ReadLine();
        }

        public static List<T> DataTableToList<T>(DataTable dt)
        {
            List<T> lists = new List<T>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T t = Activator.CreateInstance<T>();
                //Get all properties of the current T object
                PropertyInfo[] propertys = t.GetType().GetProperties();
                //Iterating out each attribute object through attribute set
                foreach (PropertyInfo item in propertys)
                {
                    #region == Original acquisition ==
                    //Loop out each column of DataTable
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //Determine whether the current iterated property name is consistent with the column name of the iterated DataTable
                        if (item.Name.ToLower().Equals(dt.Columns[j].ColumnName.ToLower()))
                        {
                            //Determine whether the single column value of the current DataTable is null
                            if (dt.Rows[i][j] != DBNull.Value)
                                //Assign the single column value of the current DataTable to the matching property, otherwise assign a null value
                                item.SetValue(t, (dt.Rows[i][j]), null);
                            else
                                item.SetValue(t, null, null);
                            break;
                        }

                    }
                    #endregion


                }
                lists.Add(t);
            }
            return lists;
        }

        public static List<T> DataTableToList2<T>(DataTable dt)
        {
            List<T> lists = new List<T>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T t = Activator.CreateInstance<T>();
                //Get all properties of the current T object
                PropertyInfo[] propertys = t.GetType().GetProperties();
                //Iterating out each attribute object through attribute set
                foreach (PropertyInfo item in propertys)
                {
                    int index = dt.Columns.IndexOf(item.Name);// Column names in datatable are not case sensitive,
                    if (index>=0)//dt.Columns.Contains(item.Name), it is faster to read the value with index
                    {
                        //Determine whether the single column value of the current DataTable is null
                        if (dt.Rows[i][index] != DBNull.Value)
                            //Assign the single column value of the current DataTable to the matching property, otherwise assign a null value
                            item.SetValue(t, (dt.Rows[i][index]), null);
                        else
                            item.SetValue(t, null, null);

                    }

                }
                lists.Add(t);
            }
            return lists;
        }

        public static List<T> DataTableToList3<T>(DataTable dt)
        {
            List<T> lists = new List<T>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T t = Activator.CreateInstance<T>();
                //Get all properties of the current T object
                PropertyInfo[] propertys = t.GetType().GetProperties();
                //Iterating out each attribute object through attribute set
                foreach (PropertyInfo item in propertys)
                {
                    //Int index = DT. Columns. Indexof (item. Name); / / column names in datatable are not case sensitive,
                    if (dt.Columns.Contains(item.Name))//dt.Columns.Contains(item.Name), it is faster to read the value with index
                    {
                        //Determine whether the single column value of the current DataTable is null
                        if (dt.Rows[i][item.Name] != DBNull.Value)
                            //Assign the single column value of the current DataTable to the matching property, otherwise assign a null value
                            item.SetValue(t, (dt.Rows[i][item.Name]), null);
                        else
                            item.SetValue(t, null, null);

                    }

                }
                lists.Add(t);
            }
            return lists;
        }

    }

Call test results:

   DataTableTest test = new DataTableTest();
            test.TestColumsNameIngore();
  ! [when the data volume is large, the difference is more obvious] (http://img.blog.csdn.net/20180105174828511? Watermark / 2 / text / ahr0cdovl2jsb2cuy3nkbi5uzxqvcwlxaw5nbgk = / font / 5a6l5l2t / fontsize / 400 / fill / i0jbqkfcma = / resolve / 70 / gravity / Southeast)    

Posted by crazylegseddie on Tue, 05 May 2020 10:18:51 -0700