Example #1
0
        /// <summary>
        /// 转化为DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="varlist"></param>
        /// <param name="fn"></param>
        /// <returns></returns>
        /// 创建时间:2018-5-29
        public static DataTable ToDataTable <T>(this IEnumerable <T> varlist, CreateRowDelegate <T> fn)
        {
            DataTable dtReturn = new DataTable();

            // column names
            PropertyInfo[] oProps = null;
            // Could add a check to verify that there is an element 0
            foreach (T rec in varlist)
            {
                // Use reflection to get property names, to create table, Only first time, others will follow
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable <>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            return(dtReturn);
        }
        /// <summary>
        /// T
        /// </summary>
        /// <param name="varlist">the IEnumerable list</param>
        /// <param name="fn"> function to create new object such as rec => new object[] {query}</param>
        /// <typeparam name="T"></typeparam>
        /// <returns>DataTable Object</returns>
        public static DataTable ToAdoTable <T>(this IEnumerable <T> varlist, CreateRowDelegate <T> fn)
        {
            var dtReturn = new DataTable();
            // Could add a check to verify that there is an element 0

            var enumerable = varlist as T[] ?? varlist.ToArray();
            T   topRec     = enumerable.ElementAt(0);

            // Use reflection to get property names, to create table

            // column names

            var oProps = topRec.GetType().GetProperties();

            foreach (var pi in oProps)
            {
                var colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable <>)))
                {
                    colType = colType.GetGenericArguments()[0];
                }

                dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
            }

            foreach (var rec in enumerable)
            {
                var dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) ?? DBNull.Value;
                }
                dtReturn.Rows.Add(dr);
            }

            return(dtReturn);
        }
Example #3
0
        public static DataTable ToDataTable <T>(this IEnumerable <T> varlist, CreateRowDelegate <T> fn, string[] fields = null)
        {
            //存表的列名
            DataTable dtReturn = new DataTable();

            // 访问属性元素
            PropertyInfo[] oProps = null;

            // 判断属性元素大于0就遍历

            foreach (T rec in varlist)
            {
                // 用反射来获取属性名,创建表,只执行第一次
                if (oProps == null)
                {
                    //得到公有属性
                    oProps = ((Type)rec.GetType()).GetProperties();
                    //遍历属性中的数据
                    foreach (PropertyInfo pi in oProps)
                    {
                        if (fields != null && string.IsNullOrWhiteSpace(fields.FirstOrDefault(s => s.ToLower().Equals(pi.Name.ToLower()))))
                        {
                            continue;
                        }
                        //获取属性的名称与类型


                        Type colType = pi.PropertyType;

                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable <>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }

                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                //将数据填充到行中
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    if (fields != null && string.IsNullOrWhiteSpace(fields.FirstOrDefault(s => s.ToLower().Equals(pi.Name.ToLower()))))
                    {
                        continue;
                    }
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
                }

                dtReturn.Rows.Add(dr);
            }

            return(dtReturn);
        }
        /// <summary>
        /// Convert  list to Data Table
        /// </summary>
        /// <typeparam name="T">Target Class</typeparam>
        /// <param name="iEnumerable">list you want to convert it to Data Table</param>
        /// <param name="fn">Delegate Function to Create Row</param>
        /// <returns>Data Table That Represent List data</returns>
        internal static DataTable ToDataTable <T>(IEnumerable <T> iEnumerable, CreateRowDelegate <T> fn)
        {
            if (fn is null)
            {
                throw new ArgumentNullException(nameof(fn));
            }

            using var toReturn = new DataTable();
            // Could add a check to verify that there is an element 0
            IEnumerable <T> enumerable = iEnumerable.ToList();
            T topRec = enumerable.ElementAtOrDefault(0);

            if (topRec == null)
            {
                return(toReturn);
            }

            // Use reflection to get property names, to create table
            // column names

            PropertyInfo[] oProps = topRec.GetType().GetProperties();

            foreach (PropertyInfo pi in oProps)
            {
                Type pt = pi.PropertyType;
                if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    pt = System.Nullable.GetUnderlyingType(pt);
                }
                toReturn.Columns.Add(pi.Name, pt);
            }

            foreach (T rec in enumerable)
            {
                DataRow dr = toReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    object o = pi.GetValue(rec, null);
                    if (o == null)
                    {
                        dr[pi.Name] = DBNull.Value;
                    }
                    else
                    {
                        dr[pi.Name] = o;
                    }
                }
                toReturn.Rows.Add(dr);
            }

            return(toReturn);
        }
Example #5
0
        /// <summary>
        /// Convert  list to Data Table
        /// </summary>
        /// <typeparam name="T">Target Class</typeparam>
        /// <param name="varlist">list you want to convert it to Data Table</param>
        /// <param name="fn">Delegate Function to Create Row</param>
        /// <returns>Data Table That Represent List data</returns>
        public static DataTable ToADOTable <T>(this IEnumerable <T> varlist, CreateRowDelegate <T> fn)
        {
            DataTable toReturn = new DataTable();

            // Could add a check to verify that there is an element 0
            T TopRec = varlist.ElementAtOrDefault(0);

            if (TopRec == null)
            {
                return(toReturn);
            }

            // Use reflection to get property names, to create table
            // column names

            PropertyInfo[] oProps = ((Type)TopRec.GetType()).GetProperties();

            foreach (PropertyInfo pi in oProps)
            {
                Type pt = pi.PropertyType;
                if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    pt = Nullable.GetUnderlyingType(pt);
                }
                toReturn.Columns.Add(pi.Name, pt);
            }

            foreach (T rec in varlist)
            {
                DataRow dr = toReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    object o = pi.GetValue(rec, null);
                    if (o == null)
                    {
                        dr[pi.Name] = DBNull.Value;
                    }
                    else
                    {
                        dr[pi.Name] = o;
                    }
                }
                toReturn.Rows.Add(dr);
            }

            return(toReturn);
        }