public static IEnumerable <T> ConvertToEntityCollection <T>(this DataTable dt) where T : new()
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return(Enumerable.Empty <T>());
            }

            IList <T> entities = new List <T>();

            IEnumerable <PropertyInfo> properties = typeof(T).GetProperties();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T entity = new T();

                foreach (PropertyInfo property in properties)
                {
                    var attributes = property.GetCustomAttributes(typeof(EntityPropertyExtensionAttribute), false);

                    if (attributes != null && attributes.Any())
                    {
                        EntityPropertyExtensionAttribute attribute = (EntityPropertyExtensionAttribute)attributes.First();
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (dt.Columns[j].ColumnName == attribute.DBTableColumnName)
                            {
                                property.SetValue(entity, dt.Rows[i][j].ConvertSimpleType(property.PropertyType), null);
                                break;
                            }
                        }
                    }
                }

                entities.Add(entity);
            }

            return(entities);
        }
        public static DataTable ConverToTable <T>(this IEnumerable <T> collection, DataTable dt)
        {
            if (collection == null || !collection.Any())
            {
                return(dt);
            }

            IEnumerable <PropertyInfo> properties = typeof(T).GetProperties();

            foreach (T entity in collection)
            {
                DataRow dr = dt.NewRow();
                foreach (PropertyInfo property in properties)
                {
                    var attributes = property.GetCustomAttributes(typeof(EntityPropertyExtensionAttribute), false);

                    if (attributes != null && attributes.Any())
                    {
                        EntityPropertyExtensionAttribute attribute = (EntityPropertyExtensionAttribute)attributes.First();

                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            if (dt.Columns[i].ColumnName == attribute.ViewTableColumnName)
                            {
                                object val = property.GetValue(entity, null);

                                if (val == null)
                                {
                                    dr[dt.Columns[i].ColumnName] = string.Empty;
                                    break;
                                }

                                if (!string.IsNullOrEmpty(attribute.ToStringVal))
                                {
                                    MethodInfo mInfo = property.PropertyType.GetMethod("ToString",
                                                                                       BindingFlags.Public | BindingFlags.Instance,
                                                                                       null,
                                                                                       CallingConventions.Any,
                                                                                       new Type[] { typeof(string) },
                                                                                       null);

                                    if (mInfo != null)
                                    {
                                        dr[dt.Columns[i].ColumnName] = mInfo.Invoke(val, new[] { attribute.ToStringVal });
                                    }
                                    else
                                    {
                                        dr[dt.Columns[i].ColumnName] = val.ToString();
                                    }
                                }
                                else
                                {
                                    dr[dt.Columns[i].ColumnName] = val.ToString();
                                }

                                break;
                            }
                        }
                    }
                }

                dt.Rows.Add(dr);
            }

            return(dt);
        }