C # convert list <> generic set to datatable

Source: Internet
Author: User
Use Case: Save the data in the DataGrid table in the easy UI of the page as a JSON string, and pass in C # Through Ajax and ashx to parse the string-type JSON string into a list <> generic set, due to business needs, data from different local SQL tables and page HTML data (imported through WebService) are put together to process data.

Conversion Method:

Public class listtodatatable {public listtodatatable () {} public static datatable listtodatatable <t> (list <t> entitys) {// check that the object set cannot be empty if (entitys = NULL | entitys. count <1) {return New datatable ();} // retrieve all propertie type entitytype = entitys [0] of the first object. getType (); propertyinfo [] entityproperties = entitytype. getproperties (); // generate the datatable structure // In the production code, cache the generated datatable structure. Here, the datatable dt is omitted. = New datatable ("DT"); For (INT I = 0; I <entityproperties. length; I ++) {// DT. columns. add (entityproperties [I]. name, entityproperties [I]. propertytype); DT. columns. add (entityproperties [I]. name);} // Add all entity to datatable foreach (Object entity in entitys) {// check that all entities are of the same type if (entity. getType ()! = Entitytype) {Throw new exception ("the element types of the set to be converted are inconsistent");} object [] entityvalues = new object [entityproperties. length]; for (INT I = 0; I <entityproperties. length; I ++) {entityvalues [I] = entityproperties [I]. getvalue (entity, null);} DT. rows. add (entityvalues) ;}return DT ;}}

 

1. Page code

  var rows = $(‘#tb_Master‘).datagrid(‘getRows‘);        this.dataArray = new Array();        for (this.i = 0; i < rows.length; i++) {            this.model = {                PT_PCURR: rows[i].PT_PCURR,                MATNR: rows[i].MATNR            }            dataArray.push(model);        }        var json = $.toJSON(dataArray);

The following JSON string [{"pt_pcurr": "1.00", "matnr": "8" },{ "pt_pcurr": "0.80", "matnr ": "8" },{ "pt_pcurr": "-0.20", "matnr": "8"}]

2. Convert string to list <> generic set

List <ddmx> listmx = NULL; If (! String. isnullorempty (Param [1]) {listmx = tools. jsontools. jsonstringtolist <ddmx> (Param [1]);} [serializable] // This entity class is freely created, class ddmx {private string _ matnr; private decimal _ pt_pcurr; Public decimal pt_pcurr {set {_ pt_pcurr = value ;} get {return _ pt_pcurr;} Public String matnr {set {_ matnr = value;} get {return _ matnr ;}}}

If you need the jsonstringtolist method, you can leave a message or search for it on the Internet.

 

3. Convert the list <> set to datatable.

DataTable dt = new DataTable();dt = ListToDatatable.ListToDataTable(listMX);

 

C # convert list <> generic set to datatable

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.