Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tsubaki961
Last active November 24, 2016 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki961/c2cef2ffa184b08b189ddf6c2ae504c7 to your computer and use it in GitHub Desktop.
Save tsubaki961/c2cef2ffa184b08b189ddf6c2ae504c7 to your computer and use it in GitHub Desktop.
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