当前位置:网站首页>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的字段一样
边栏推荐
- Express の Hello World
- MySQL-- Entity Framework Code First(EF Code First)
- Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
- float
- Deep understanding of kotlin collaboration context coroutinecontext
- 4. use ibinder interface flexibly for short-range communication
- Deep Learning with Pytorch- A 60 Minute Blitz
- About MySQL Boolean and tinyint (1)
- 12. problem set: process, thread and JNI architecture
- Why must redis exist in distributed systems?
猜你喜欢

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

Rew acoustic test (I): microphone calibration

Solution to the eighth training competition of 2020 Provincial Games

Pit encountered by fastjason

Esp32 things (II): sharpening the knife without mistaking firewood - make preparations before project development

Clickhouse installation (quick start)

Deep understanding of continuation principle

Summary of Android knowledge points and common interview questions

ES6 learning path (II) let & const

Linear-gradient()
随机推荐
Coredata acquisition in swift sorting, ascending, descending
Guilin robust medical acquired 100% equity of Guilin Latex to fill the blank of latex product line
AutoUpdater. Net client custom update file
What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
Concatapter tutorial
List set export excel table
4. use ibinder interface flexibly for short-range communication
Understanding of MVVM and MVC
Talking about kotlin process exception handling mechanism
Installation, use and explanation of vulnerability scanning tool OpenVAS
2020-11-02
Rew acoustic test (III): generate test signal
Application of hongruan face recognition
Solution to pychart's failure in importing torch package
Esp32 things (VIII): music playing function of function development
Initialize static resource demo
Maxiouassigner of mmdet line by line interpretation
100 lines of code and a voice conversation assistant
What are the SQL add / delete / modify queries?
Mmdet line by line code interpretation of positive and negative sample sampler