Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
private List<T> ConvertToList<T>(DataTable dt)
{
var columns = dt.Columns.Cast<DataColumn>()
.Select(c => c.ColumnName)
.ToList();
var properties = typeof(T).GetProperties();
return dt.AsEnumerable().Select(row => GetT<T>(row, columns, properties)).ToList();
}
private T GetT<T>(DataRow dr, List<string> ColumnNames, PropertyInfo[] properties)
{
var ResT = Activator.CreateInstance<T>();
foreach (var pro in properties)
{
if (ColumnNames.Contains(pro.Name))
pro.SetValue(ResT, dr[pro.Name]);
}
return ResT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment