C # Converts a DataTable into a list

Source: Internet
Author: User
Tags reflection

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

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.