1:
2: public static class Util
3: {
4: public static DataTable ToDataTable(this IEnumerable varlist, CreateRowDelegate fn)
5: {
6: DataTable dtReturn = new DataTable();
7: // column names
8: PropertyInfo[] oProps = null;
9: // Could add a check to verify that there is an element 0
10: foreach (T rec in varlist)
11: {
12: // Use reflection to get property names, to create table, Only first time, others will follow
13: if (oProps == null)
14: {
15: oProps = ((Type)rec.GetType()).GetProperties();
16: foreach (PropertyInfo pi in oProps)
17: {
18: // Note that we must check a nullable type else method will throw and error
19: Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable)))
20: {
21: // Since all the elements have same type you can just take the first element and get type
22: colType = colType.GetGenericArguments()[0];
23: }
24: dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
25: }
26: }
27: DataRow dr = dtReturn.NewRow();
28: //Iterate through each property in PropertyInfo
29: foreach (PropertyInfo pi in oProps)
30: {
31: // Handle null values accordingly
32: dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
33: }
34: dtReturn.Rows.Add(dr);
35: }
36: return (dtReturn);
37: }
38: public delegate object[] CreateRowDelegate(T t);
39: }
1:
2: public ISingleResult SelectMethod(){
3: //Get DAL Instance DALInstance
4: return DALInstance.GetSomeData(){}
5: }
6: public DataTable GetSomeData()
7: {
8: ISingle result = //code to get result;
9: return (result.ToDataTable(rec => new object[] { result}));
10: }