일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- CNN
- DFS
- pycharm
- 삼성소프트웨어멤버십
- Button
- SciPy
- javascript
- index
- ipad
- Splunk
- Series
- install
- GT-S80
- RNN
- mean
- synology
- dataframe
- mariadb
- Python
- Lotto
- pandas
- Numpy
- imread
- 알고리즘
- LSTM
- pip
- GitHub
- keras
- SPL
- E-P1
Archives
- Today
- Total
잠토의 잠망경
[c#] from DataTable To List<T> 본문
DataTable에서 List<T> 로 변환하기
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace WCF_Util
{
public class MyConverter
{
public static List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
public static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
//pro.SetValue(obj, dr[column.ColumnName], null);
pro.SetValue(obj, Convert.ChangeType(dr[column.ColumnName], pro.PropertyType), null);
else
continue;
}
}
return obj;
}
}
}
사용법
void GetDtCallBack(object sender, ServiceReference1.GetDT_OracleCompletedEventArgs e)
{
List<SHOPS> temp = MyConverter.ConvertDataTable<SHOPS>(e.Result);
MessageBox.Show(e.Result.Rows.Count.ToString()+" 건");
}
Comments