Avatar of Olukayode Oluwole
Olukayode Oluwole
Flag for Canada asked on

How can i pass my list to a DataTable and Load same to a Grid

I have a List in my c#  application prepared using LINQ

The list is generated and i need to pass it to a ListToDataTable  processor in my Library

before loading to a Grid

The  List to Datatable Script  in my Library is as shown below

[] public static class ListToDataTableProcessor
    {

        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;
        }
    }[/code]

My application LINQ  script that generates the List is shown below with the last 2 lines flagged.

I am trying to pass the list to the Processor and return a DataTable  (see screen below)

List to DataTable Script
The list generation is ok. I just do not know how to correct the error reported

and how to pass the list to the processor and get the returned Table

I will be Grateful for any Help

Thank

Olukay
* LINQC#

Avatar of undefined
Last Comment
Olukayode Oluwole

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
it_saige

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Olukayode Oluwole

ASKER
Thanks .

You always made my day with your proof of concept
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23