2010-12-01 4 views
0

Мне нужно преобразовать Ienumerable в набор данных. Для этого мне нужно написать общее fnction для преобразования любого типа IEnumberable в Dataset. Для этого мне нужно установить анонимный тип. Вот код.net Как установить анонимный тип для IEnumberable

public static DataTable ToDataTable(Object alist) 
     { 
      String AssemblyName = "DataAccessLayer"; 
      String ClassName = "sptblSystemPreference_GetListResult"; 
      Type ObjType = Type.GetType(ClassName + "," + AssemblyName); 

//Below line working fine... But how can i implement ObjType in the IEnumberable 
      IEnumerable<DataAccessLayer.sptblSystemPreference_GetListResult> objList1 = (IEnumerable<DataAccessLayer.sptblSystemPreference_GetListResult>)alist; 
      List<DataAccessLayer.sptblSystemPreference_GetListResult> objList = objList1.ToList(); 

      DataTable dt = new DataTable(); 

      if (objList[0] != null) 
      { 
       dt.TableName = objList[0].GetType().Name; 
       System.Reflection.PropertyInfo[] propInfo = objList[0].GetType().GetProperties(); 
       for (int PropertyCount = 0; PropertyCount < propInfo.Length; PropertyCount++) 
       { 
        dt.Columns.Add(propInfo[PropertyCount].Name); 
       } 
       for (int row = 0; row < objList.Count; row++) 
       { 
        DataRow dr; 
        dr = dt.NewRow(); 
        for (int PropertyCount = 0; PropertyCount < propInfo.Length; PropertyCount++) 
        { 

         Object obj = propInfo[PropertyCount].GetValue(objList[row], null); 
         if(obj!=null) 
          dr[PropertyCount] = obj; 
        } 
        dt.Rows.Add(dr); 
       } 
      } 
      return dt;   

     } 
    } 
+0

Вы должны задать это по адресу http://stackoverflow.com – Graviton 2010-12-01 08:03:50

+0

Ниже мой ответ, но вы также должны быть более точным в том, что вы просите. В том, что вы написали выше, нет никаких сомнений. Поэтому очень сложно понять, в чем проблема. – 2010-12-01 08:55:43

ответ

1

Я реализовал это для преобразования IList<T> к DataTable и задокументированы на моем блоге:

public static class ListExtensions 
{ 
    public static DataTable ToDataTable<T>(this IList<T> list) 
    { 
     IList<PropertyInfo> properties = list.GetPropertiesOfObjectInList(); 
     DataTable resultTable = CreateTable(properties); 

     foreach(var item in list) 
     { 
      var row = CreateRowFromItem<T>(resultTable, item); 
      resultTable.Rows.Add(row); 
     } 

     return resultTable; 
    } 

    private static DataTable CreateTable(IList<PropertyInfo> properties) 
    { 
     DataTable resultTable = new DataTable(); 
     foreach (var property in properties) 
     { 
      resultTable.Columns.Add(property.Name, property.PropertyType); 
     } 
     return resultTable; 
    } 

    public static IList<PropertyInfo> GetPropertiesOfObjectInList<T>(this IList<T> list) 
    { 
     return typeof(T).GetProperties().ToList(); 
    } 

    private static DataRow CreateRowFromItem<T>(DataTable resultTable, T item) 
    { 
     var row = resultTable.NewRow(); 
     var properties = item.GetType().GetProperties().ToList(); 
     foreach (var property in properties) 
     { 
      row[property.Name] = property.GetValue(item, null); 
     } 
     return row; 
    } 
} 

Это позволяет писать код как DataTable yourTable = yourList.ToDataTable(). Вы можете прочитать об этом здесь: Generic list to DataTable

Смежные вопросы