Mostrando las entradas con la etiqueta Listas. Mostrar todas las entradas

Extender un IList para devolver un Datatable

 

  1. /// <summary>  
  2. /// Exiende un metodo sobre una Lista para devolver un Dataset  
  3. /// con las columnas del objeto de la lista  
  4. /// </summary>  
  5. /// <typeparam name="T"></typeparam>  
  6. /// <param name="pList"></param>  
  7. /// <returns></returns>  
  8. public static DataTable ConvertTo<T>( this IList<T> pList )  
  9. {  
  10.   DataTable table = CreateTable<T>();  
  11.   Type entityType = typeof( T );  
  12.   PropertyDescriptorCollection properties = TypeDescriptor.GetProperties( entityType );  
  13.   foreach ( T item in pList )  
  14.   {  
  15.        DataRow row = table.NewRow();  
  16.        foreach ( PropertyDescriptor prop in properties )  
  17.             row[prop.Name] = prop.GetValue( item );  
  18.        table.Rows.Add( row );  
  19.   }  
  20.   return table;  
  21. }  
  22. /// <summary>  
  23. /// Devuelve un item de tipo T  
  24. /// </summary>  
  25. /// <typeparam name="T"></typeparam>  
  26. /// <param name="pRow">The p row.</param>  
  27. /// <returns></returns>  
  28. private static T CreateItem<T>( DataRow pRow )  
  29. {  
  30.   T obj = default( T );  
  31.   if ( pRow == null )  
  32.        return obj;  
  33.   obj = Activator.CreateInstance<T>();  
  34.   foreach ( DataColumn column in pRow.Table.Columns )  
  35.   {  
  36.        PropertyInfo prop = obj.GetType().GetProperty( column.ColumnName );  
  37.        object value = pRow[column.ColumnName];  
  38.        prop.SetValue( obj, value, null );  
  39.   }  
  40.   return obj;  
  41. }  
  42. /// <summary>  
  43. /// Crea un DataTable con columnas del tipo T  
  44. /// </summary>  
  45. /// <typeparam name="T"></typeparam>  
  46. /// <returns></returns>  
  47. private static DataTable CreateTable<T>()  
  48. {  
  49.   Type entityType = typeof( T );  
  50.   DataTable table = new DataTable( entityType.Name );  
  51.   PropertyDescriptorCollection properties = TypeDescriptor.GetProperties( entityType );  
  52.   foreach ( PropertyDescriptor prop in properties )  
  53.        table.Columns.Add( prop.Name, prop.PropertyType );  
  54.   return table;  
  55. }
lunes, febrero 07, 2011
Posted by Carlos

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í:

  1. /// <summary>  
  2. /// Extiende un metodo sobre el objeto Datatable para devolver un IList  
  3. /// </summary>  
  4. public static IList<T> ConvertTo<T>(this DataTable pDataTable) where T : new()
  5. {
  6.     PropertyInfo[] entityImportProperties = typeof(T).GetProperties();
  7.     IList<T> colEntityImportList = new List<T>();
  8.     if (pDataTable == null)
  9.         return null;
  10.     if (pDataTable.Rows.Count == 0)
  11.         return colEntityImportList;
  12.     foreach (DataRow actualRow in pDataTable.Rows)
  13.     {
  14.         T objNewImportEntity = new T();
  15.         foreach (PropertyInfo propertyEntityImport in entityImportProperties)
  16.             if (propertyEntityImport.CanRead)
  17.                 for (int i = 0; i <= pDataTable.Columns.Count - 1; i++)
  18.                     if (pDataTable.Columns[i].ColumnName == propertyEntityImport.Name)
  19.                         if (actualRow[propertyEntityImport.Name] == null)
  20.                         {
  21.                             if (actualRow[propertyEntityImport.Name] == DBNull.Value)
  22.                                 propertyEntityImport.SetValue(objNewImportEntity, null, null);
  23.                             else
  24.                                 propertyEntityImport.SetValue(objNewImportEntity, actualRow[propertyEntityImport.Name], null);
  25.                             break;
  26.                         }
  27.         colEntityImportList.Add(objNewImportEntity);
  28.     }
  29.     return colEntityImportList;
  30.  
  31.     DataTable vSystemModulesTable = SystemManager.GetDataTable();
  32.     IList<SystemModule> vColSystem = vSystemModulesTable.ConvertTo<SystemModule>();
  33. }

Y se usa, de esta manera:

  1. DataTable vSystemModulesTable = SystemManager.GetDataTable();
  2. IList<SystemModule> vColSystem = vSystemModulesTable.ConvertTo<SystemModule>();
martes, febrero 01, 2011
Posted by Carlos

Populares!

- Copyright © - Oubliette - -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -