2016-08-30 2 views
-3
DataTable tbl = new DataTable(); 
tbl.Columns.Add("Column"); 
for (int i = 0; i < 61; i++) 
    tbl.Rows.Add(i.ToString()); 


       DataTable[] splittedtables = tbl.AsEnumerable() 
        .Select((row, index) => new { row, index }) 
        .GroupBy(x => x.index/12) // integer division, the fractional part is truncated 
        .Select(g => g.Select(x => x.row).CopyToDataTable()) 
        .ToArray(); 

       DataTable dtarr1 = splittedtables[0]; 
  • これは私のコードですが、正常に動作していますが、最も効率的な方法が必要です。あなたがデータテーブルにリストを変換したいあなたのコメントから
+0

正確に何をしたいですか? –

+0

コードレビュー(作業コードの最適化を含む)をしたい場合は、http://codereview.stackexchange.com/ – JanDotNet

答えて

1

:ここ

は、私はこれがためである

ConvertToDataTable

public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) 
    { 

     return new ObjectShredder<T>().Shred(source, null, null); 
    } 

public class ObjectShredder<T> 
{ 
    private FieldInfo[] _fi; 
    private PropertyInfo[] _pi; 
    private Dictionary<string, int> _ordinalMap; 
    private Type _type; 

    public ObjectShredder() 
    { 
     _type = typeof(T); 
     _fi = _type.GetFields(); 
     _pi = _type.GetProperties(); 
     _ordinalMap = new Dictionary<string, int>(); 
    } 

    public DataTable Shred(IEnumerable<T> source, DataTable table, LoadOption? options) 
    { 
     if (typeof(T).IsPrimitive) 
     { 
      return ShredPrimitive(source, table, options); 
     } 


     if (table == null) 
     { 
      table = new DataTable(typeof(T).Name); 
     } 

     // now see if need to extend datatable base on the type T + build ordinal map 
     table = ExtendTable(table, typeof(T)); 

     table.BeginLoadData(); 
     using (IEnumerator<T> e = source.GetEnumerator()) 
     { 
      while (e.MoveNext()) 
      { 
       if (options != null) 
       { 
        table.LoadDataRow(ShredObject(table, e.Current), (LoadOption)options); 
       } 
       else 
       { 
        table.LoadDataRow(ShredObject(table, e.Current), true); 
       } 
      } 
     } 
     table.EndLoadData(); 
     return table; 
    } 

    public DataTable ShredPrimitive(IEnumerable<T> source, DataTable table, LoadOption? options) 
    { 
     if (table == null) 
     { 
      table = new DataTable(typeof(T).Name); 
     } 

     if (!table.Columns.Contains("Value")) 
     { 
      table.Columns.Add("Value", typeof(T)); 
     } 

     table.BeginLoadData(); 
     using (IEnumerator<T> e = source.GetEnumerator()) 
     { 
      Object[] values = new object[table.Columns.Count]; 
      while (e.MoveNext()) 
      { 
       values[table.Columns["Value"].Ordinal] = e.Current; 

       if (options != null) 
       { 
        table.LoadDataRow(values, (LoadOption)options); 
       } 
       else 
       { 
        table.LoadDataRow(values, true); 
       } 
      } 
     } 
     table.EndLoadData(); 
     return table; 
    } 

    public DataTable ExtendTable(DataTable table, Type type) 
    { 
     // value is type derived from T, may need to extend table. 
     foreach (FieldInfo f in type.GetFields()) 
     { 
      if (!_ordinalMap.ContainsKey(f.Name)) 
      { 
       DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name] 
        : table.Columns.Add(f.Name); 
       _ordinalMap.Add(f.Name, dc.Ordinal); 
      } 
     } 
     foreach (PropertyInfo p in type.GetProperties()) 
     { 
      if (!_ordinalMap.ContainsKey(p.Name)) 
      { 
       DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name] 
        : table.Columns.Add(p.Name); 
       _ordinalMap.Add(p.Name, dc.Ordinal); 
      } 
     } 
     return table; 
    } 

    public object[] ShredObject(DataTable table, T instance) 
    { 

     FieldInfo[] fi = _fi; 
     PropertyInfo[] pi = _pi; 

     if (instance.GetType() != typeof(T)) 
     { 
      ExtendTable(table, instance.GetType()); 
      fi = instance.GetType().GetFields(); 
      pi = instance.GetType().GetProperties(); 
     } 

     Object[] values = new object[table.Columns.Count]; 
     foreach (FieldInfo f in fi) 
     { 
      values[_ordinalMap[f.Name]] = f.GetValue(instance); 
     } 

     foreach (PropertyInfo p in pi) 
     { 
      values[_ordinalMap[p.Name]] = p.GetValue(instance, null); 
     } 
     return values; 
    } 





} 
+0

を試してみてください。 – Webruster

1
public static DataTable create_DataTable_From_Generic_Class(Type t) 
    { 
     DataTable d = new DataTable(); 
     FieldInfo[] fI = t.GetFields(); 
     for(int i = 0; i < fI.Length; i++) 
     { 
      DataColumn dC = new DataColumn(fI[i].Name, fI[i].FieldType); 
      d.Columns.Add(dC); 
     } 
     return d; 
    } 
    public static object[] Create_Datatable_Row_From_Generic_Class(Type t, object instance,DataTable dt) 
    { 

     FieldInfo[] f = t.GetFields(); 
     object[] ret = new object[f.Length]; 
     for (int i = 0; i < dt.Columns.Count; i++) 
     { 
      ret[i] = t.GetField(dt.Columns[i].ColumnName).GetValue(instance); 

     } 
     return ret; 

    } 

を使用しています一般的な方法であり、フィールド。プロパティと、変換する他のタイプを繰り返します。

関連する問題