datatable vb.net code example

Example 1: tipar datatable in dto c#

Criando a classe para converter o DataTable para List

A seguir crie a classe Conversor com o método estático ConverterParaLista():

 using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace WF_DataTable_Objetos
{
    public static class Conversor
    {
        public static List ConverterParaLista(DataTable dt)
        {
            var columnNames = dt.Columns.Cast().Select(c => c.ColumnName.ToLower()).ToList();
            var properties = typeof(T).GetProperties();
            return dt.AsEnumerable().Select(row => {
                var objT = Activator.CreateInstance();
                foreach (var pro in properties)
                {
                    if (columnNames.Contains(pro.Name.ToLower()))
                    {
                        try
                        {
                            pro.SetValue(objT, row[pro.Name]);
                        }
                        catch (Exception)
                        { }
                    }
                }
                return objT;
            }).ToList();
        }
    }
}

Example 2: datatables.net

Javascript123$(document).ready( function () {    $('#myTable').DataTable();} );

Tags:

Misc Example