Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I'm trying to get the dataTable using NHibernate cos I need to bind dataTable in reportviewer, but NHibernate always return IList and i don't know how to convert into DataTable, so if anyone knows, pls help me!!

C#
tdsReport.Purchase.Merge(purchaseDT); // ??? how to get DataTable using NHibernate
Posted

1 solution

Try with this method

C#
public static DataTable ToDataTable<t>(this IList<t> data)
    {
        PropertyDescriptorCollection properties = 
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        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;
    }</t></t>


Source: http://stackoverflow.com/questions/564366/generic-list-to-datatable[^]


Update:

C#
public static class ExtensionMethods
{
    public static DataTable ToDataTable<T>(this IList<T> data)
    {
        PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        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;
    }
}


and use it like this:

C#
List<Purchase> a = new List<Purchase>();
DataTable t = a.ToDataTable<Purchase>();



Cheers
 
Share this answer
 
v2
Comments
Doyle Raymond 26-Mar-12 6:42am    
if u don't mind, pls explain why PropertyDescriptorCollection is used and others ?? Thanks a lot, Mario Majcica...
:)
Mario Majčica 26-Mar-12 7:30am    
Well TypeDescriptor.GetProperties() method returns the collection of properties for a specified type (in this case type T (generic)) in form of a PropertyDescriptorCollection. Once we have all properties of your IList type, we will create a table and add a column for each property we retrieved earlier. Then we will recurese the data in the list and add a new row and respective data to the datatable. This is a generic extension method and can be used with any IList. Ex. DataTable t = a.ToDataTable<Purchase>(); where a is at ex. List<Purchase> a = new List<Purchase>();

Cheers
Doyle Raymond 26-Mar-12 6:42am    
or links i can learn ??
Mario Majčica 26-Mar-12 7:43am    
You can get more info's here:

http://www.codeproject.com/Articles/261639/Extension-Methods-in-NET
http://www.abhisheksur.com/2011/02/get-reflected-use-typedescriptor.html

etc.

What are you specifically interested in?
Doyle Raymond 26-Mar-12 10:01am    
by the way, thanks a lot for your help.. but i gave u wrong info that what i need is DataTable from DataSet.. not from System.Data.DataSet..
sorry, i don't know how to call it...
Convert IList into DataSet DataTable using NHibernate query...
and i am developing in Window Form...
:(

could u help again.. ?? Mario Majcica..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900