使用此类转换为数据表,
public class DataTable : IEnumerable
{
public DataTable(IEnumerable<Dictionary<string, object>> source)
{
if (source != null)
{
var firstItem = source.FirstOrDefault();
if (firstItem != null)
{
foreach (var key in firstItem)
{
Columns.Add(new DataColumn() { ColumnName = key.Key, DataType = key.Value.GetType() });
}
foreach (var item in source)
{
var row = new DataRow();
foreach (var key in item)
{
row[key.Key] = key.Value;
}
Rows.Add(row);
}
}
}
}
List<DataColumn> columns = null;
public List<DataColumn> Columns
{
get
{
if (columns == null)
{
columns = new List<DataColumn>();
}
return columns;
}
}
List<DataRow> rows = null;
public List<DataRow> Rows
{
get
{
if (rows == null)
{
rows = new List<DataRow>();
}
return rows;
}
}
public object NewRow()
{
if (queryable != null)
{
return Activator.CreateInstance(queryable.ElementType);
}
return null;
}
#region IEnumerable Members
IQueryable queryable = null;
public IEnumerator GetEnumerator()
{
if (queryable == null)
{
var type = ClassFactory.Instance.GetDynamicClass(this.Columns.Select(c => new DynamicProperty(c.ColumnName, c.DataType)));
var propertyInfos = type.GetProperties().ToList();
var list = (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
foreach (var row in this.Rows)
{
var item = Activator.CreateInstance(type);
propertyInfos.ForEach(p => p.SetValue(item, row[p.Name], null));
list.Add(item);
}
queryable = list.AsQueryable();
}
return queryable.GetEnumerator();
}
#endregion
public IList ToList()
{
var enumerator = GetEnumerator();
var list = (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(queryable.ElementType));
while (enumerator.MoveNext())
{
list.Add(enumerator.Current);
}
return list;
}
}
public class DataColumn
{
public DataColumn()
{
DataType = typeof(object);
}
public Type DataType { get; set; }
public string ColumnName { get; set; }
}
public class DataRow : Dictionary<string, object>
{
//
}