当前位置:网站首页>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的字段一样
边栏推荐
- [cmake] make command cannot be executed normally
- Numpy (time date and time increment)
- Use of Baidu face recognition API
- Application of hongruan face recognition
- Influencing factors of echo cancellation for smart speakers
- Find the number that appears only once in the array
- List set export excel table
- Esp32 (IX): OTA function of function development
- Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
- Numpy (data type)
猜你喜欢

8.8 heap insertion and deletion

ES6 learning path (III) deconstruction assignment

Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)

Find the number that appears only once in the array

Rew acoustic test (VI): signal and measurement

Talk about how the kotlin collaboration process establishes structured concurrency

Implementing custom drawer component in quick application

Raspberry pie 4B no screen installation system and networking using VNC wireless projection function

Handwriting sorter component

float
随机推荐
Interviewer: do you understand the principle of recyclerview layout animation?
Esp32 (7): I2S and I2C drivers for function development
Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)
[paid promotion] collection of frequently asked questions, FAQ of recommended list
Deep Learning with Pytorch- A 60 Minute Blitz
The elegant combination of walle and Jianbao
Solution to the eighth training competition of 2020 Provincial Games
Use of Baidu face recognition API
Using OpenCV Net for image restoration
Pass anonymous function to simplification principle
ACM intensive training graph theory exercise 3 in the summer vacation of 2020 [problem solving]
Abstract factory pattern
Common query and aggregation of ES
Deep Learning with Pytorch- A 60 Minute Blitz
POJ 1753 flip game (DFS 𞓜 bit operation)
Influencing factors of echo cancellation for smart speakers
What are the SQL add / delete / modify queries?
Mmdet line by line deltaxywhbboxcoder
Experience of an acmer
Challenge transform() 2D