天天看點

IList轉DataTable

        #region 資料集互操作

        /// <summary>

        /// 将集合類轉換成DataTable

        /// </summary>

        /// <param name="list">集合</param>

        /// <returns></returns>

        public static DataTable ToDataTable(IList list)

        {

            DataTable result = new DataTable();

            if (list.Count > 0)

            {

                PropertyInfo[] propertys = list[0].GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)

                {

                    result.Columns.Add(pi.Name, pi.PropertyType);

                }

                for (int i = 0; i < list.Count; i++)

                    ArrayList tempList = new ArrayList();

                    foreach (PropertyInfo pi in propertys)

                    {

                        object obj = pi.GetValue(list[i], null);

                        tempList.Add(obj);

                    }

                    object[] array = tempList.ToArray();

                    result.LoadDataRow(array, true);

            }

            return result;

        }

        /// 将泛型集合類轉換成DataTable

        /// <typeparam name="T">集合項類型</typeparam>

        /// <returns>資料集(表)</returns>

        public static DataTable ToDataTable<T>(IList<T> list)

            return  ConvertX.ToDataTable<T>(list, null);

        /// <param name="propertyName">需要傳回的列的列名</param>

        public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)

            List<string> propertyNameList = new List<string>();

            if (propertyName != null)

                propertyNameList.AddRange(propertyName);

                    if (propertyNameList.Count == 0)

                        result.Columns.Add(pi.Name, pi.PropertyType);

                    else 

                        if (propertyNameList.Contains(pi.Name))

                            result.Columns.Add(pi.Name, pi.PropertyType);

                        if (propertyNameList.Count == 0)

                        {

                            object obj = pi.GetValue(list[i], null);

                            tempList.Add(obj);

                        }

                        else

                            if (propertyNameList.Contains(pi.Name))

                            {

                                object obj = pi.GetValue(list[i], null);

                                tempList.Add(obj);

                            }

        #endregion

    }

    public class ConvertXToDataTableTester : ITest

    {

        #region ITest 成員

        public string Name

            get { return new ConvertXToDataTableTester().GetType().Name; }

        public void StartRuntime()

            //Test1(ToDataTable<>):boundary

            CollectionBase<Item> items1 = new CollectionBase<Item>();

            items1.Add(new Item("item1_1", 1));

            items1.Add(new Item("item1_2", "null"));

            items1.Add(new Item("item1_3", 3));

            DataTable dt = ConvertX.ToDataTable<Item>(items1);

            foreach (DataRow dr in dt.Rows)

                Console.WriteLine("{0},{1}", dr["text"].ToString(), dr[1].ToString());

            //Test2(ToDataTable):

            ArrayList items2 = new ArrayList();

            items2.Add(new Item("item2_1", 3));

            items2.Add(new Item("item2_2", 4));

            items2.Add(new Item("item2_3", 5));

            DataTable dt2 = ConvertX.ToDataTable(items2);

            foreach (DataRow dr in dt2.Rows)

            //Test3(ToDataTable<>):

            IList<Item> items3 = new CollectionBase<Item>();

            items3.Add(new Item("item3_1", 3));

            items3.Add(new Item("item3_2", 4));

            items3.Add(new Item("item3_3", 5));

            DataTable dt3 = ConvertX.ToDataTable<Item>(items3, "Text");

            foreach (DataRow dr in dt3.Rows)

                //Console.WriteLine("{0},{1}", dr["text"].ToString(), dr[1].ToString());

                Console.WriteLine("{0}", dr["text"].ToString());

            //Test4(ToDataTable<>):Error with error column name

            try

                foreach (DataRow dr in dt.Rows)

                    Console.WriteLine(dr["errorName"].ToString());

            catch (Exception ex)

                Console.WriteLine(ex.Message);

    public class Item

        public Item(string text, object value)

            this.text = text;

            this.value = value;

        private string text;

        public string Text

            get

                return this.text;

            set

                this.text = value;

        private object value;

        public object Value

                return this.value;

                this.value = value;

IList轉DataTable

    public interface ITest

IList轉DataTable
IList轉DataTable
IList轉DataTable

{

IList轉DataTable
IList轉DataTable

        string Name 

IList轉DataTable

{ get; }

IList轉DataTable

        void StartRuntime();

IList轉DataTable
IList轉DataTable

using System;

IList轉DataTable

using System.Collections.Generic;

IList轉DataTable

using System.Text;

IList轉DataTable

using yd.Base;

IList轉DataTable

using yd.Base.Collections.Generic;

IList轉DataTable

using yd.Util;

IList轉DataTable
IList轉DataTable

namespace ConsoleApplicationTester

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

    class Program

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

        static void Main(string[] args)

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

            bool debugging = false;

IList轉DataTable
IList轉DataTable

            CollectionBase<ITest> testers = new CollectionBase<ITest>();

IList轉DataTable

            //testers.Add(new QuarterTester());

IList轉DataTable

            //testers.Add(new ConvertXToDataTableTester());

IList轉DataTable
IList轉DataTable

            foreach (ITest tester in testers)

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

                if (debugging == false)

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

                    try

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

                        tester.StartRuntime();

IList轉DataTable

                        Console.WriteLine("___________________________________");

IList轉DataTable

                        Console.WriteLine(tester.Name + " succeed!");

IList轉DataTable
IList轉DataTable

                    catch (Exception ex)

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

                        Console.WriteLine(tester.Name + " with errors!");

IList轉DataTable

                        Console.WriteLine("____________The error is under the line!__________");

IList轉DataTable

                        Console.WriteLine(ex.Message);

IList轉DataTable

                        Console.WriteLine("!!!!!!!!!!!!!!!!!!!!@@@@@@@!!!!!!!!!!!!!!!!!!!!");

IList轉DataTable
IList轉DataTable
IList轉DataTable

                else

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

                    tester.StartRuntime();

IList轉DataTable

                    Console.WriteLine("___________________________________");

IList轉DataTable

                    Console.WriteLine(tester.Name + " succeed!");

IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable
IList轉DataTable

}

IList轉DataTable