Codigos Fonte, Artigos e Dicas
Converter List para DataSet

Opa! olha eu ai novamente =D, tentando passar algo de (INUTIL) UTIL pra vocês.
Hoje vou mostrar como converter uma List para DataSet, é Super Simples, vamos lá:
public static class ListConvertor
{
public static DataSet ConvertToDataSet<T>(IList list)
{
DataSet dataSet = new DataSet();
CreateDataSet(dataSet, typeof(T), false);
FillDataSet(typeof(T), list, dataSet, -1);
CreateRelations(dataSet, typeof(T), null);
return dataSet;
}
///
/// Create the structure for all the tables in the data set
///
/// Data set in which tables will be created
/// Type of which dataset has to be created
/// Whether current type is a child table
private static void CreateDataSet(DataSet dataSet, Type type, bool isChildTable)
{
DataTable dataTable = new DataTable(type.Name);
//Create the ID columns for having relation in the tables
dataTable.Columns.Add(new DataColumn("ID2", typeof(int)));
if (isChildTable)
{
dataTable.Columns.Add(new DataColumn("ParentID2", typeof(int)));
}
// Create the structure for the data tables to be
// added in the the data set
foreach (PropertyInfo pInfo in type.GetProperties())
{
if (pInfo.PropertyType.IsGenericType &&
(pInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)
|| pInfo.PropertyType.GetGenericTypeDefinition() == typeof(IList<>)))
{
// If associate lists are there make then another table
CreateDataSet(dataSet,pInfo.PropertyType.GetGenericArguments()[0],true);
}
else
{
dataTable.Columns.Add(new DataColumn(pInfo.Name, pInfo.PropertyType));
}
}
//Add the table to the dataset
dataSet.Tables.Add(dataTable);
}
///
/// Fill all the tables of data set with data in the respective list
///
/// Type of which datatable is to be filled
/// List of data
/// Data Set in which data tables will be filled with data
/// ID of parent record. If -1 one then no parent
private static void FillDataSet(Type type, IList list, DataSet dataSet, int parentID)
{
PropertyInfo[] propertyInfos = type.GetProperties();
DataTable dataTable = dataSet.Tables[type.Name];
int id = dataTable.Rows.Count + 1;
foreach (object item in list)
{
DataRow row = dataTable.NewRow();
// Set new id and related parent id
row["ID"] = id;
if (parentID != -1)
row["ParentID"] = parentID;
// Load all the data from the properties of the type
// and save them into the datatable
foreach (PropertyInfo info in propertyInfos)
{
if (info.PropertyType.IsGenericType &&
(info.PropertyType.GetGenericTypeDefinition() == typeof(List<>)
|| info.PropertyType.GetGenericTypeDefinition() == typeof(IList<>)))
{
IList subList = (IList)info.GetValue(item, null);
if (subList != null && subList.Count > 0)
{
FillDataSet(subList[0].GetType(),
subList,
dataSet, id);
}
}
else
{
row[info.Name] = info.GetValue(item, null);
}
}
dataTable.Rows.Add(row);
id++;
}
}
///
/// Creates the relation between the tables according to the
/// type and parent table on field ID and ParentID
///
/// Data set containing parent and child table
/// Type of the list
/// Parent table to which relations has to be done
private static void CreateRelations(DataSet dataSet, Type type, DataTable parentTable)
{
DataTable dataTable = dataSet.Tables[type.Name];
// If parent table exsits then create relation
// with child table on field Parent ID
if (parentTable != null)
{
dataSet.Relations.Add(
new DataRelation(parentTable.TableName + "_ID_"
+ "PARENTID_" + dataTable.TableName,
parentTable.Columns["ID"],
dataTable.Columns["ParentID"]));
}
// Check for other lists under current object
// go for another relation if exists
foreach (PropertyInfo pInfo in type.GetProperties())
{
if (pInfo.PropertyType.IsGenericType &&
(pInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)
|| pInfo.PropertyType.GetGenericTypeDefinition() == typeof(IList<>)))
{
// If associate lists are there make then another table
CreateRelations(dataSet,pInfo.PropertyType.GetGenericArguments()[0],dataTable);
}
}
}
}
Vamos para as explicações:
ArrayList sequence = new ArrayList(); IList list = (IList)sequence; DataSet ds = ListConvertor.ConvertToDataSet<LancamentosDetalhado>(list);
Como nos queremos passar uma List para DataSet, vamos forçar um ArrayList ser uma lista, já que ele extend de IList, estamos fazendo isso com o cast (IList), logo após aplicamos o metodo STATIC, ListConvertor.ConvertToDataSet<LancamentosDetalhado>(list);,
pois a assinatura do metodo esta como static, logo só pode ser chamado a partir da classe.
Simples assim.
Nenhum post foi relacionado a este
| Imprimir artigo | Este artigo foi escrito por paulodiogo em 24 24UTC setembro 24UTC 2009 às 12:23, e está arquivado em C# Dicas, Tecnicas de Programação. Siga quaisquer respostas a este artigo através do RSS 2.0. Você pode deixar uma resposta ou fazer um trackback do seu próprio site. |

há 1 mês atrás
Paulo, segue abaixo outro método para conversão de List para Dataset.
public static DataSet ToDataSet(this IList list)
{
Type elementType = typeof(T);
DataSet ds = new DataSet();
DataTable t = new DataTable();
//adciona coluna na tabela para cada propriedade pública da Lista
foreach (var propInfo in elementType.GetProperties())
{ t.Columns.Add(propInfo.Name, propInfo.PropertyType); }
//por meio de cada propriedade, é valorizado os campos
foreach (T item in list)
{
DataRow row = t.NewRow();
foreach (var propInfo in elementType.GetProperties())
{ row[propInfo.Name] = propInfo.GetValue(item, null); }
t.Rows.Add(row);
}
ds.Tables.Add(t);
return ds;
}
Abraços e até a próxima =)
[Reply]