DataTable to List & lt; Model & gt; general class, datatablemodel

Source: Internet
Author: User

DataTable to List <Model> common class, datatablemodel

/// <Summary> // DataTable to List <Model> generic class [entity conversion helper class] /// </summary> public class ModelConvertHelper <T> where T: new () {public static IList <T> ConvertToModel (DataTable dt) {// defines the set IList <T> ts = new List <T> (); // obtain the model Type type = typeof (T); string tempName = ""; foreach (DataRow dr in dt. rows) {T t = new T (); // obtain the public attributes of this model. PropertyInfo [] propertys = t. getType (). getProperties (); foreach (PropertyInfo pi in pro Pertys) {tempName = pi. Name; // check whether the DataTable Contains this column if (dt. Columns. Contains (tempName) {// determine whether this attribute has Setif (! Pi. CanWrite) continue; object value = dr [tempName]; if (value! = DBNull. Value) pi. SetValue (t, value, null) ;}} ts. Add (t) ;}return ts ;}}

// Convert DataTable to IList <View_TableListModel>
IList <View_TableListModel> vlistTable = ModelConvertHelper <View_TableListModel>. ConvertToModel (dtTable );


A List of a single table is returned for a Multi-Table query by using LinQ <> result set. But how can I convert the list to a List that is received by a able type on the page?

ToDataTable () is the method written by others. You can write one by yourself. As follows:
Public staic class ListExtension
{
Public static DataTable ToDataTable (this List <T_D_MaintainRegion> list)
{
DataTable dt = new DataTable ();
Dt. Columns. Add (xxxxxxx );
..............
Foreach (T_D_MaintainRegion item in list)
{
Dt. Rows. Add (item. xxx, item. xxx ......);
}
Return dt;
}
}

Net data table to json

To convert Dt to Json, follow these steps:
First, create an object class for the corresponding table. This class mark can be serialized.
First, query the database to obtain table data (for example, obtain a table)
Compile the record to instantiate the table entity class and assign values based on the field name.
The object class is serialized in json format to obtain data in Json format. (If you need multiple data items, you only need one List, and then serialize the List)
The core of my previous work is to convert DT into a collection of entity classes.
/// <Summary>
/// DataTable converts a data table to a List <Model> generic class.
/// </Summary>
/// <Typeparam name = "T"> <Model type/typeparam>
Public static IList <T> ConvertToModel (DataTable dt)
{
// Define a set
IList <T> ts = new List <T> ();
// Obtain the model type
Type type = typeof (T );
String tempName = "";
Foreach (DataRow dr in dt. Rows)
{
T t = new T ();
// Obtain the public attributes of this model
PropertyInfo [] propertys = t. GetType (). GetProperties ();
Foreach (PropertyInfo pi in propertys)
{
TempName = pi. Name;
// String objType = pi. PropertyType. Name;
// Check whether the DataTable contains this column
If (dt. Columns. Contains (tempName ))
{
// Determine whether the property has a Setter
If (! Pi. CanWrite) continue;
Object value = dr [tempName];
If (value! = DBNull. Value)
{
If (value is DateTime) value = value. ToString ();
... The remaining full text>

Related Article

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.