当前位置:网站首页>DataTableToModelList实体类
DataTableToModelList实体类
2022-06-30 09:32:00 【小生一枚】
public List<T> DataTableToModelList<T>(DataTable table)
{
List<T> list = new List<T>();
T t = default(T);
PropertyInfo[] propertypes = null;
string tempName = string.Empty;
foreach (DataRow row in table.Rows)
{
t = Activator.CreateInstance<T>();
propertypes = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertypes)
{
tempName = pro.Name;
if (table.Columns.Contains(tempName))
{
object value = row[tempName].ToString();
if (value.GetType() == typeof(System.DBNull))
{
value = null;
}
pro.SetValue(t, value, null);
}
}
list.Add(t);
}
return list;
}
下边展示实际中的用法
public void GetData()
{
using (MySqlHelper helper = new MySqlHelper(conn))
{
List<Data> info= new List<Data>();
DataSet ds = helper.ExecuteDataSet("select * from Data", CommandType.Text);
info= DataTableToModelList<Data>(ds.Tables[0]);
}
}
public class Data
{
public string Name {
get; set; }
public int Id{
get; set; }
}
这样就实现了把Datatable转换为指定ModelList的过程,注意Table的字段要和ModelList的字段一样
边栏推荐
猜你喜欢

Installation, use and explanation of vulnerability scanning tool OpenVAS

Esp32 things (3): overview of the overall system design

Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)

Talk about how the kotlin collaboration process establishes structured concurrency

Terminal -- Zsh of terminal three swordsmen

Talk about how the kotlin process started?

2021-10-20

Clickhouse installation (quick start)

桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白

Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
随机推荐
AutoUpdater. Net client custom update file
Numpy (time date and time increment)
Use V-IF with V-for
What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
100 lines of code and a voice conversation assistant
5. Messager framework and imessager interface
Function simplification principle: save if you can
Niuke rearrangement rule taking method
Small program learning path 1 - getting to know small programs
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
Terminal -- Zsh of terminal three swordsmen
Guilin robust medical acquired 100% equity of Guilin Latex to fill the blank of latex product line
Pytorch for former Torch users - Tensors
Simple redis lock
Tutorial for beginners of small programs day01
Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)
Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)
[paid promotion] collection of frequently asked questions, FAQ of recommended list
The elegant combination of walle and Jianbao
Talk about the job experience of kotlin cooperation process