일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 삼성소프트웨어멤버십
- pandas
- Series
- DFS
- mariadb
- GitHub
- mean
- javascript
- synology
- SPL
- Splunk
- RNN
- keras
- index
- E-P1
- ipad
- Python
- pycharm
- pip
- install
- CNN
- LSTM
- Lotto
- Button
- dataframe
- GT-S80
- imread
- SciPy
- Numpy
- 알고리즘
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