9
avril
2007
IEnumerable To DataTable
avril
2007
J’ai essayé d’améliorer l’exemple de Mike Taulty.
Voici le code wue je propose :
namespace System.Collections.Generic
{
public static class IEnumerableExtension
{
public static DataTable ToDataTable<T>(this IEnumerable<T> enumerable)
{
return ToDataTable(enumerable, null);
}
public static DataTable ToDataTable<T>(this IEnumerable<T> enumerable, Func<IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> filterPropertiesDelegate)
{
DataTable table = new DataTable();
IEnumerable<PropertyInfo> props = typeof(T).GetProperties();
if (filterPropertiesDelegate != null)
props = filterPropertiesDelegate(props);
foreach (PropertyInfo prop in props)
table.Columns.Add(prop.Name, prop.PropertyType);
foreach (T t in enumerable)
{
var row = table.NewRow();
foreach (PropertyInfo prop in props)
row[prop.Name] = prop.GetValue(t, null);
table.Rows.Add(row);
}
return table;
}
}
}
{
public static class IEnumerableExtension
{
public static DataTable ToDataTable<T>(this IEnumerable<T> enumerable)
{
return ToDataTable(enumerable, null);
}
public static DataTable ToDataTable<T>(this IEnumerable<T> enumerable, Func<IEnumerable<PropertyInfo>, IEnumerable<PropertyInfo>> filterPropertiesDelegate)
{
DataTable table = new DataTable();
IEnumerable<PropertyInfo> props = typeof(T).GetProperties();
if (filterPropertiesDelegate != null)
props = filterPropertiesDelegate(props);
foreach (PropertyInfo prop in props)
table.Columns.Add(prop.Name, prop.PropertyType);
foreach (T t in enumerable)
{
var row = table.NewRow();
foreach (PropertyInfo prop in props)
row[prop.Name] = prop.GetValue(t, null);
table.Rows.Add(row);
}
return table;
}
}
}
et mon programme de test :
class Program
{
static void Main(string[] args)
{
Test[] test = { new Test("un", "one", 1), new Test("deux", "two", 2), new Test("trois", "three", 3) };
WriteRows(test.ToDataTable());
Console.WriteLine();
WriteRows(test.ToDataTable(props => from prop in props
where prop.PropertyType == typeof(string)
select prop));
Console.ReadLine();
}
static void WriteRows(DataTable table)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
Console.Write(String.Format("{0} : {1} ", column.ColumnName, row[column]));
Console.WriteLine();
}
}
}
class Test
{
public Test(string s1, string s2, int i1)
{
TestStr1 = s1;
TestStr2 = s2;
TestInt1 = i1;
}
public string TestStr1 { get; set; }
public string TestStr2 { get; set; }
public int TestInt1 { get; set; }
}
{
static void Main(string[] args)
{
Test[] test = { new Test("un", "one", 1), new Test("deux", "two", 2), new Test("trois", "three", 3) };
WriteRows(test.ToDataTable());
Console.WriteLine();
WriteRows(test.ToDataTable(props => from prop in props
where prop.PropertyType == typeof(string)
select prop));
Console.ReadLine();
}
static void WriteRows(DataTable table)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
Console.Write(String.Format("{0} : {1} ", column.ColumnName, row[column]));
Console.WriteLine();
}
}
}
class Test
{
public Test(string s1, string s2, int i1)
{
TestStr1 = s1;
TestStr2 = s2;
TestInt1 = i1;
}
public string TestStr1 { get; set; }
public string TestStr2 { get; set; }
public int TestInt1 { get; set; }
}