none
Converter um datatable em uma lista RRS feed

Respostas

  • deste jeito:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace listas
    {
        public static class Util
        {
            public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()
            {
                var dataList = new List<TSource>();
    
                const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
                var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)
                                     select new
                                     {
                                         Name = aProp.Name,
                                         Type = Nullable.GetUnderlyingType(aProp.PropertyType) ??
                                 aProp.PropertyType
                                     }).ToList();
                var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns
                                         select new
                                         {
                                             Name = aHeader.ColumnName,
                                             Type = aHeader.DataType
                                         }).ToList();
                var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();
    
                foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
                {
                    var aTSource = new TSource();
                    foreach (var aField in commonFields)
                    {
                        PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
                        var value = (dataRow[aField.Name] == DBNull.Value) ?
                        null : dataRow[aField.Name]; //if database field is nullable
                        propertyInfos.SetValue(aTSource, value, null);
                    }
                    dataList.Add(aTSource);
                }
                return dataList;
            }
    }

    exemplo de uso:

       public static List<Tipos> getTipo(BD bd)
            {
                DataSet ds_dataset = new DataSet();
                SqlParameter[] parm = null;
                List<Tipos> Tipos= new List<Tipos>();
    
                try
                {
                    ds_dataset = bd.getDataSet("GetTipos", CommandType.StoredProcedure, parm);
    
                    
                    if (ds_dataset != null)
                        if (ds_dataset.Tables[0].Rows.Count > 0)
                            Tipos= Util.ToList<Tipos>(ds_dataset.Tables[0]);
    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return Tipos;
            }

    att


    William John Adam Trindade
    Analyste-programmeur
    ----------------------------------------------------------

    • Marcado como Resposta Eduardo Hipolito quinta-feira, 17 de setembro de 2015 20:06
    quinta-feira, 17 de setembro de 2015 20:01
    Moderador

Todas as Respostas

  • deste jeito:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace listas
    {
        public static class Util
        {
            public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()
            {
                var dataList = new List<TSource>();
    
                const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
                var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)
                                     select new
                                     {
                                         Name = aProp.Name,
                                         Type = Nullable.GetUnderlyingType(aProp.PropertyType) ??
                                 aProp.PropertyType
                                     }).ToList();
                var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns
                                         select new
                                         {
                                             Name = aHeader.ColumnName,
                                             Type = aHeader.DataType
                                         }).ToList();
                var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();
    
                foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
                {
                    var aTSource = new TSource();
                    foreach (var aField in commonFields)
                    {
                        PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
                        var value = (dataRow[aField.Name] == DBNull.Value) ?
                        null : dataRow[aField.Name]; //if database field is nullable
                        propertyInfos.SetValue(aTSource, value, null);
                    }
                    dataList.Add(aTSource);
                }
                return dataList;
            }
    }

    exemplo de uso:

       public static List<Tipos> getTipo(BD bd)
            {
                DataSet ds_dataset = new DataSet();
                SqlParameter[] parm = null;
                List<Tipos> Tipos= new List<Tipos>();
    
                try
                {
                    ds_dataset = bd.getDataSet("GetTipos", CommandType.StoredProcedure, parm);
    
                    
                    if (ds_dataset != null)
                        if (ds_dataset.Tables[0].Rows.Count > 0)
                            Tipos= Util.ToList<Tipos>(ds_dataset.Tables[0]);
    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return Tipos;
            }

    att


    William John Adam Trindade
    Analyste-programmeur
    ----------------------------------------------------------

    • Marcado como Resposta Eduardo Hipolito quinta-feira, 17 de setembro de 2015 20:06
    quinta-feira, 17 de setembro de 2015 20:01
    Moderador
  • Puxa ! Muito obrigado !!!!
    quinta-feira, 17 de setembro de 2015 20:06