This article explains how to use C # to convert a DataTable data source to a list generic collection (known as the T type), and a small partner in need can refer to the
When you use a three-tier architecture to develop a Web site, you want to convert a DataTable object to a list object, and then find data on the web to summarize a more convenient way to implement it-using reflection.
Ideas:
Initializes a list object to get all the properties of T, Initializes a T object that iterates through all the properties and assigns the T object if the value of the corresponding property is in the DataTable, and if there are no columns, check that the data model is defined incorrectly (property names are not case-sensitive when compared to column names). Add a T object to the list object
Overall code:
-c站">
Property information obtained by reflection
-c语言">
Column information in a DataTable, in contrast to the above figure, we will find that the first letter in the attribute is uppercase, and the column name is the camel named, the first letter is lowercase, but with single-step debugging can be found dt. The return value of Columns.contanis (Tempname) is true, which proves that this comparison is case-insensitive
-cf">
Get to T object information
-ch">
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
|