/// <summary> /// Returns the entity data present in properties /// as an array /// </summary> /// <param name="entityType"></param> /// <param name="entity"></param> /// <returns></returns> static object[] GetEntityData(Type entityType, object entity) { List <object> data = new List <object>(); List <EntityPropertyInfo> entityProperties = EntityInfoCache.Get(entityType); if (null == entity) { entityProperties .Where(p => !p.IsEntity) .ToList() .ForEach(p => data.Add(null)); } else { // Take all simple columns and add its data entityProperties .Where(p => !p.IsEntity) .ToList() .ForEach(p => data.Add(GetEntityPropertyData(entity, p))); } // Now add data for each entity property column entityProperties .Where(p => p.IsEntity) .ToList() .ForEach(p => data.AddRange(GetEntityData(p.PropertyInfo.PropertyType, p.PropertyAccess.Get(entity)))); return(data.ToArray()); }
/// <summary> /// Gets all properties that are entities in the current parent entity /// </summary> /// <param name="entity"></param> /// <returns></returns> internal static IEnumerable <EntityPropertyInfo> GetEntityProperties(this object entity) { List <EntityPropertyInfo> entityProperties = EntityInfoCache.Get(entity.GetType()); foreach (EntityPropertyInfo property in entityProperties) { if (property.IsEntity) { yield return(property); } } }
/// <summary> /// Converts the list to a dataset /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entityList"></param> /// <returns></returns> public static DataSet CreateDataSet <T>(List <T> entityList) { DataSet dataSet = new DataSet(); Type entityType = entityList.GetEntityTypeFromEntityList(); List <EntityPropertyInfo> entityProperties = EntityInfoCache.Get(entityType); // Part 1 // Generate data columns for all simple and single entity properties List <DataColumn> dataColumns = GetDataColumns(entityProperties); DataTable defaultTable = new DataTable(); dataSet.Tables.Add(defaultTable); dataColumns.ForEach(c => defaultTable.Columns.Add(c)); // Populate the default table with data LoadData(defaultTable, entityList); // Part 2 // Create data tables for all entity lists // and populate them with data columns T entityInstance = (from e in entityList where null != e select e).FirstOrDefault(); entityInstance .GetEntityListProperties() .Where(p => null != p.DataTable) .OrderBy(p => p.DataTable.TableIndex) .ToList() .ForEach(p => dataSet.Tables .Add(p.PropertyName) .Columns.AddRange( GetDataColumns(EntityInfoCache.Get(p.PropertyInfo.PropertyType) ) .ToArray() ) ); // Now popuplate each extra table with the details entityInstance .GetEntityListProperties() .Where(p => null != p.DataTable) .ToList() .ForEach(p => entityList.ForEach( e => LoadData(dataSet.Tables[p.PropertyName], ((IList)p.PropertyAccess.Get(e))) ) ); return(dataSet); }
/// <summary> /// Creates all column property maps for a given entity /// </summary> /// <param name="sourceTable"></param> /// <param name="entityType"></param> /// <returns></returns> public static Dictionary <Type, ColumnPropertyMap> CreateMapsForEntity(DataTable sourceTable, Type entityType) { Dictionary <Type, ColumnPropertyMap> entityColumnMaps = new Dictionary <Type, ColumnPropertyMap>(); List <EntityPropertyInfo> properties = EntityInfoCache.Get(entityType); // The first map is for the entity entityColumnMaps.Add(entityType, Create(sourceTable, entityType)); // The next maps are for entity properties in the parent entity object foreach (EntityPropertyInfo entityProperty in properties) { if (entityProperty.IsEntity) { entityColumnMaps.Add(entityProperty.PropertyInfo.PropertyType, Create(sourceTable, entityProperty.PropertyInfo.PropertyType)); } } return(entityColumnMaps); }
/// <summary> /// Gets a list of data columns from the given entity properties /// </summary> /// <param name="entityProperties"></param> /// <returns></returns> static List <DataColumn> GetDataColumns(List <EntityPropertyInfo> entityProperties) { List <DataColumn> dataColumns = new List <DataColumn>(); // Add all the simple property data columns entityProperties .Where(p => !p.IsEntity) .ToList() .ConvertAll(p => CreateDataColumn(p)) .ForEach(c => dataColumns.Add(c)); // Now add all the complex data columns entityProperties .Where(p => p.IsEntity) .ToList() .ConvertAll(p => EntityInfoCache.Get(p.PropertyInfo.PropertyType)) .ForEach(pList => dataColumns.AddRange(GetDataColumns(pList))); return(dataColumns); }
/// <summary> /// Creates a column property mapping for the specified /// data table and entity /// </summary> /// <param name="sourceTable"></param> /// <param name="entityType"></param> /// <returns></returns> public static ColumnPropertyMap Create(DataTable sourceTable, Type entityType) { ColumnPropertyMap map = new ColumnPropertyMap(); List <EntityPropertyInfo> properties = EntityInfoCache.Get(entityType); foreach (DataColumn column in sourceTable.Columns) { foreach (EntityPropertyInfo property in properties) { if (property.IsEntity || property.IsEntityList) { continue; } if (property.EffectiveDataColumnName.ToUpper().Trim() == column.ColumnName.ToUpper().Trim()) { map.Add(column.ColumnName, property); } } } return(map); }