This works:
var Result = from e in actual.Elements
select new
{
Key = e.Key,
You can write a simple extension method that takes any IEnumerable
, uses reflection to get the PropertyDescriptor
s associated with T, and creates a DataColumn
for each
public static DataTable PropertiesToDataTable(this IEnumerable source)
{
DataTable dt = new DataTable();
var props = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in props)
{
DataColumn dc = dt.Columns.Add(prop.Name,prop.PropertyType);
dc.Caption = prop.DisplayName;
dc.ReadOnly = prop.IsReadOnly;
}
foreach (T item in source)
{
DataRow dr = dt.Rows.NewRow();
foreach (PropertyDescriptor prop in props)
dr[prop.Name] = prop.GetValue(item);
dt.Rows.Add(dr);
}
return dt;
}