Пример #1
0
        /// <summary>
        /// LIST实体转换表格
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="entityList"></param>
        /// <returns></returns>
        public static DataTable ToDataTable <T>(this IEnumerable <T> entityList)
        {
            if (typeof(T).IsClass && typeof(T).IsGenericType)
            {
                Type      entityType         = typeof(T);
                DataTable entityMappingTable = EntityExtensions.GetEntityMappingTable(entityType);
                if (entityList == null || entityList.Count() == 0)
                {
                    return(entityMappingTable);
                }

                var constructor = entityType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                  .OrderBy(c => c.GetParameters().Length).First();
                //取当前构造函数的参数
                var             parameters = constructor.GetParameters();
                IEnumerator <T> enumerator = entityList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    T       current = enumerator.Current;
                    DataRow dataRow = entityMappingTable.NewRow();
                    foreach (var item in parameters)
                    {
                        PropertyInfo p = current.GetType().GetProperty(item.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        if (p != null)
                        {
                            object obj = p.GetValue(current, null);
                            if (p.PropertyType.GetUnderlyingType() == typeof(DateTime))
                            {
                                obj = ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss.fff");
                            }
                            dataRow[item.Name] = ((obj == null) ? DBNull.Value : obj);
                        }
                    }
                    enumerator.Disposed();
                    entityMappingTable.Rows.Add(dataRow);
                }
                return(entityMappingTable);
            }
            else
            {
                Type      entityType         = typeof(T);
                DataTable entityMappingTable = EntityExtensions.GetEntityMappingTable(entityType);
                if (entityList == null || entityList.Count() == 0)
                {
                    return(entityMappingTable);
                }
                IEnumerator <T> enumerator = entityList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    T current = enumerator.Current;


                    //DataRow dataRow = entityMappingTable.NewRow();
                    //IEnumerator enumerator2 = entityMappingTable.Columns.GetEnumerator();
                    //while (enumerator2.MoveNext())
                    //{
                    //    DataColumn dataColumn = (DataColumn)enumerator2.Current;
                    //    if (!dataColumn.AutoIncrement)
                    //    {
                    //        if (((IEntity)current).ChangedMappingProperties.Keys.Contains(dataColumn.ColumnName))
                    //        {
                    //            object obj = ((IEntity)current).ChangedMappingProperties[dataColumn.ColumnName];
                    //            dataRow[dataColumn.ColumnName] = ((obj == null) ? DBNull.Value : obj);
                    //        }
                    //    }
                    //}

                    DataRow     dataRow     = entityMappingTable.NewRow();
                    IEnumerator enumerator2 = entityMappingTable.Columns.GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        DataColumn   dataColumn = (DataColumn)enumerator2.Current;
                        PropertyInfo p          = current.GetType().GetProperty(dataColumn.ColumnName, BindingFlags.Public | BindingFlags.Instance);
                        if (p != null)
                        {
                            object obj = p.GetValue(current, null);
                            if (p.PropertyType.GetUnderlyingType() == typeof(DateTime))
                            {
                                obj = ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss.fff");
                            }
                            dataRow[dataColumn.ColumnName] = ((obj == null) ? DBNull.Value : obj);
                        }
                    }
                    enumerator.Disposed();
                    entityMappingTable.Rows.Add(dataRow);
                }
                enumerator.Disposed();
                return(entityMappingTable);
            }
        }