I have a class:
public class MyClass
{
public uint ID { get; set; }
public List<double> Values { get; set; }
}
Then, I'm creating a list of class objects:
List<MyClass> objects = new List<MyClass>(); // then I add the data to this list
Now, I'm need to select data from objects like:
var query = objects.Select(o => new { o.ID, o.Values } ).ToList(); // this example show only ID field in DataTable
and convert result to DataTable like this:
public DataTable ConvertToDataTable<T>(IList<T> data)
{
DataTable table = new DataTable();
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
So, I need to make something like cross-tab query. How can I doing that?