C # convert DataTable to List

Source: Internet
Author: User

C # convert DataTable to List

This article explains how to use C # to convert a able data source to a List. Generic set (known T type) methods and examples. If you need them, refer

When developing a website using a three-tier architecture, you want to convert the DataTable object to List Object, so you can find information on the Internet and summarize a more convenient method to implement-use reflection.

Ideas:

Initialize a List The object obtains all T attributes and initializes a T object to traverse all attributes. If the DataTable contains the values of the corresponding attributes, the T object is assigned a value, if no corresponding Column exists, check whether the data model is incorrectly defined (the attribute name and column name are case-insensitive). Add the T object to the List. Object

Overall code:

Reflected property information obtained

The column information in the DataTable. After comparison, we will find that the first letter of the attribute is in upper case, while the column name is in the Camel name, and the first letter is in lower case. However, dt can be found through single-step debugging. columns. contanis (tempName) returns true, which proves that the comparison is case insensitive.

Get T object information

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

/// <Summary>

/// Use reflection to convert a able to a List <T> Object

/// </Summary>

/// <Param name = "dt"> DataTable object </param>

/// <Returns> List <T> set </returns>

Public static List <T> DataTableToList <T> (DataTable dt) where T: class, new ()

{

// Define a set

List <T> ts = new List <T> ();

// Define a Temporary Variable

String tempName = string. Empty;

// Traverse all data rows in the DataTable

Foreach (DataRow dr in dt. Rows)

{

T t = new T ();

// Obtain the public attributes of this model

PropertyInfo [] propertys = t. GetType (). GetProperties ();

// Traverse all attributes of the object

Foreach (PropertyInfo pi in propertys)

{

TempName = pi. Name; // assign the attribute Name to the Temporary Variable

// Check whether the DataTable contains this column (column name = Object attribute name)

If (dt. Columns. Contains (tempName ))

{

// Value

Object value = dr [tempName];

// If it is not empty, the property assigned to the object

If (value! = DBNull. Value)

{

Pi. SetValue (t, value, null );

}

}

}

// Add an object to a generic set

Ts. Add (t );

}

Return ts;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.