当前位置:网站首页>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的字段一样
边栏推荐
- ACM intensive training graph theory exercise 3 in the summer vacation of 2020 [problem solving]
- Esp32 (4): overview of the overall code architecture
- Niuke walks on the tree (ingenious application of parallel search)
- Express の Hello World
- Acquisition de 100% des actions de Guilin latex par Guilin Robust Medical pour combler le vide de la gamme de produits Latex
- 8.8 heap insertion and deletion
- Pipe pipe --namedpipe and anonymouspipe
- Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
- Design specification for smart speakers v1.0
- Rew acoustic test (II): offline test
猜你喜欢

I once met a girl whom I most wanted to take care of all my life. Later... No later

I'm late for school

Talk about the kotlin cooperation process and the difference between job and supervisorjob

9.JNI_ Necessary optimization design

Guilin robust medical acquired 100% equity of Guilin Latex to fill the blank of latex product line

桂林 穩健醫療收購桂林乳膠100%股權 填補乳膠產品線空白

What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?

Do you want the dialog box that pops up from the click?

8.8 heap insertion and deletion

Handwriting sorter component
随机推荐
Initialize static resource demo
Explanation on the use of password profiteering cracking tool Hydra
Implementing custom drawer component in quick application
Agp7.0|kts makes a reinforced plug-in
Esp32 things (x): other functions
Pytorch for former Torch users - Tensors
Bottomsheetbehavior principle of realizing the home page effect of Gaode map
Esp32 (6): Bluetooth and WiFi functions for function development
4. use ibinder interface flexibly for short-range communication
Talk about how the kotlin collaboration process establishes structured concurrency
Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)
ES6 learning road 5 symbol
Esp32 (IX): OTA function of function development
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
Installation, use and explanation of vulnerability scanning tool OpenVAS
Talk about how the kotlin process started?
Opencv learning notes-day14 drawing of image geometry (rect class rotatedrect class, rectangle drawing rectangle circle drawing circular function line drawing line function ellipse drawing elliptic fu
Numpy (data type)
Generate directory in markdown
MySQL-- Entity Framework Code First(EF Code First)