Last active
November 24, 2016 14:57
-
-
Save tsubaki961/c2cef2ffa184b08b189ddf6c2ae504c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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