Mostrando las entradas con la etiqueta Listas. Mostrar todas las entradas
Extender un IList para devolver un Datatable
- /// <summary>
- /// Exiende un metodo sobre una Lista para devolver un Dataset
- /// con las columnas del objeto de la lista
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="pList"></param>
- /// <returns></returns>
- public static DataTable ConvertTo<T>( this IList<T> pList )
- {
- DataTable table = CreateTable<T>();
- Type entityType = typeof( T );
- PropertyDescriptorCollection properties = TypeDescriptor.GetProperties( entityType );
- foreach ( T item in pList )
- {
- DataRow row = table.NewRow();
- foreach ( PropertyDescriptor prop in properties )
- row[prop.Name] = prop.GetValue( item );
- table.Rows.Add( row );
- }
- return table;
- }
- /// <summary>
- /// Devuelve un item de tipo T
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="pRow">The p row.</param>
- /// <returns></returns>
- private static T CreateItem<T>( DataRow pRow )
- {
- T obj = default( T );
- if ( pRow == null )
- return obj;
- obj = Activator.CreateInstance<T>();
- foreach ( DataColumn column in pRow.Table.Columns )
- {
- PropertyInfo prop = obj.GetType().GetProperty( column.ColumnName );
- object value = pRow[column.ColumnName];
- prop.SetValue( obj, value, null );
- }
- return obj;
- }
- /// <summary>
- /// Crea un DataTable con columnas del tipo T
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- private static DataTable CreateTable<T>()
- {
- Type entityType = typeof( T );
- DataTable table = new DataTable( entityType.Name );
- PropertyDescriptorCollection properties = TypeDescriptor.GetProperties( entityType );
- foreach ( PropertyDescriptor prop in properties )
- table.Columns.Add( prop.Name, prop.PropertyType );
- return table;
- }
Extender un Datatable para devolver un IList
Necesito pasar el contenido de un Datatable a un IList de tipo T, aprovechando la facilidad de extender clases, quedo así:
- /// <summary>
- /// Extiende un metodo sobre el objeto Datatable para devolver un IList
- /// </summary>
- public static IList<T> ConvertTo<T>(this DataTable pDataTable) where T : new()
- {
- PropertyInfo[] entityImportProperties = typeof(T).GetProperties();
- IList<T> colEntityImportList = new List<T>();
- if (pDataTable == null)
- return null;
- if (pDataTable.Rows.Count == 0)
- return colEntityImportList;
- foreach (DataRow actualRow in pDataTable.Rows)
- {
- T objNewImportEntity = new T();
- foreach (PropertyInfo propertyEntityImport in entityImportProperties)
- if (propertyEntityImport.CanRead)
- for (int i = 0; i <= pDataTable.Columns.Count - 1; i++)
- if (pDataTable.Columns[i].ColumnName == propertyEntityImport.Name)
- if (actualRow[propertyEntityImport.Name] == null)
- {
- if (actualRow[propertyEntityImport.Name] == DBNull.Value)
- propertyEntityImport.SetValue(objNewImportEntity, null, null);
- else
- propertyEntityImport.SetValue(objNewImportEntity, actualRow[propertyEntityImport.Name], null);
- break;
- }
- colEntityImportList.Add(objNewImportEntity);
- }
- return colEntityImportList;
- DataTable vSystemModulesTable = SystemManager.GetDataTable();
- IList<SystemModule> vColSystem = vSystemModulesTable.ConvertTo<SystemModule>();
- }
Y se usa, de esta manera:
- DataTable vSystemModulesTable = SystemManager.GetDataTable();
- IList<SystemModule> vColSystem = vSystemModulesTable.ConvertTo<SystemModule>();
