In C #, DataTable is converted to List & lt; T & gt; parsing,

Source: Internet
Author: User

In C #, DataTable is converted to List <T> parsing,

In. there are many places where DataTable and List <T> sets are used in the. net project. The benefits of generics: It adds great efficiency and flexibility for compiling object-oriented programs using c # language. The value type is not forcibly packed or unpacked, or the reference type is not. The conversion between the two is complicated. The primary problem is the storage method of the two. The data storage method of DataTable adopts a two-dimensional table to store data. DataTable represents a table of data in the memory. In a List set, the essence of a List is an array, which uses a linear structure to store data.

In the conversion process, the main problem lies in the processing of different types, mainly divided into two categories: Value Type and reference type.

C # The median type always contains a value of the expected type, including: Simple type (Simple types), structure type (struct types), and Enumeration type (Enumeration types ).

Simple types include: integer, Boolean, numeric (a special case of an integer), floating point, and decimal.

Integer types include sbyte, byte, short, ushort, int, uint, long, ulong, and char.

Reference Type: The reference type does not store the actual data they represent, but stores the reference of the actual data. It mainly includes: object type, class type, interface, representing Meta, string type, and array.

The conversion code is provided for reference only:

1. Type enumeration:

/// <Summary> /// type enumeration /// </summary> private enum ModelType {// Value Type Struct, Enum, // reference type String, Object, else} private static ModelType GetModelType (Type modelType) {// value Type if (modelType. isEnum) {return ModelType. enum;} // value type if (modelType. isValueType) {return ModelType. struct;} // special type of reference processing if (modelType = typeof (string) {return ModelType. string;} // special type of reference processing return modelType = typeo F (object )? ModelType. Object: ModelType. Else ;}

 

2. Specific conversion methods:

/// <Summary> // datatable to be converted to List <T> set /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "table"> </param> // <returns> </returns> public static List <T> DataTableToList <T> (DataTable table) {var list = new List <T> (); foreach (DataRow item in table. rows) {list. add (DataRowToModel <T> (item);} return list;} public static T DataRowToModel <T> (DataRow row) {T model; var type = typeof (T); var modelType = GetModelType (type); switch (modelType) {// value type case ModelType. struct: {model = default (T); if (row [0]! = Null) model = (T) row [0];} break; // value type case ModelType. Enum: {model = default (T); if (row [0]! = Null) {var fiType = row [0]. getType (); if (fiType = typeof (int) {model = (T) row [0];} else if (fiType = typeof (string )) {model = (T) Enum. parse (typeof (T), row [0]. toString () ;}} break; // reference type c # handle case ModelType as a value type for string. string: {model = default (T); if (row [0]! = Null) model = (T) row [0];} break; // The reference type directly returns the value of case ModelType in the first column of the first row. object: {model = default (T); if (row [0]! = Null) model = (T) row [0];} break; // reference type case ModelType. else: {// The reference type must instantiate the generic model = Activator. createInstance <T> (); // obtain the var modelPropertyInfos = type attribute in the model. getProperties (); // traverse each attribute of the model and assign a value to the column foreach (var pi in modelPropertyInfos) corresponding to DataRow {// obtain the attribute name var name = pi. name; if (! Row. table. columns. contains (name) | row [name] = null) continue; var piType = GetModelType (pi. propertyType); switch (piType) {case ModelType. struct: {var value = Convert. changeType (row [name], pi. propertyType); pi. setValue (model, value, null);} break; case ModelType. enum: {var fiType = row [0]. getType (); if (fiType = typeof (int) {pi. setValue (model, row [name], null);} else if (fiType = typeof (String) {var value = (T) Enum. Parse (typeof (T), row [name]. ToString (); if (value! = Null) pi. setValue (model, value, null) ;}} break; case ModelType. string: {var value = Convert. changeType (row [name], pi. propertyType); pi. setValue (model, value, null);} break; case ModelType. object: {pi. setValue (model, row [name], null);} break; case ModelType. else: throw new Exception ("conversion of this type is not supported"); default: throw new Exception ("unknown type") ;}} break; default: model = default (T ); break;} return model ;}

The preceding operations have corresponding processing methods for different types.

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.