c# list to datatable

Just add this function and call it, it will convert List to DataTable.

public static DataTable ToDataTable<T>(List<T> items)
{
        DataTable dataTable = new DataTable(typeof(T).Name);

        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Defining type of data column gives proper data table 
            var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name, type);
        }
        foreach (T item in items)
        {
           var values = new object[Props.Length];
           for (int i = 0; i < Props.Length; i++)
           {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
           }
           dataTable.Rows.Add(values);
      }
      //put a breakpoint here and check datatable
      return dataTable;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c# dataTable to array list convertir list en datatable c# list object to datatable c# create datatable from list of objects c# object list to datatable datatable to object list c# convert from datatable to list c# convert list to datatable c# linq datatables to list c# linq list to datatable c# convert datatable to object list in c# c# convert list&lt;dynamic&gt; to datatable convert list dynamic to datatable c# list dynamic to datatable c# .net list to datatable c# datatable to list dynamic best way to convert datatable to list c# how convert datatable to list in c# how to cast list to datatable in c# c# list of object to datatable c# convert from list to datatable convert object list to datatable in c# convert datatable to list&lt;T&gt; in c# datatable.tolist c# c# convert datatable to list c# list of class into datatable c# convert list datatable convert list item to datatable c# list c# to datatable list item to datatable c# convert datatable to list of objects c# datatable to list of objects c# c# list from datatable column list of objects to datatable c# asp.net c# datatable to list convert list of an object to datatable c# list of object to datatable c# how to convert list object to datatable in c# mvc how to convert list to datatable in c# mvc convert list of objects to datatable c# Convert a list to datatable in C# from dataTable to list box c# c# datatable from list of Datarow convert datatable to list&lt;object in c# convert datatable to list class c# convert a datatable to list in c# how to convert list to datatable create datatable from list c# datatable to list c# linq datalist to datatable c# how to convert list of objects to datatable in c# c# list&lt;T&gt; to datatable convert datatable to a list in c# datatable to list object c# c# list int to datatable datatable to model list c# how to convert list into datatable in c# convert list to datatable in c#.net list datarow to datatable c# c# list object to datatable how to get data from list to datatable in c# string list to datatable c# convert list object to datatable c# c# datatable to list C&quot; Datatable from LIst datatable to list c# list to datatable datatable to list string c# .net datatable to list c# list of objects to datatable c# datatable from list c# create datatable from list how to convert a datatable to list in c# how to convert datatable to list in c# how to create datatable from list object in c# convert dynamic list to datatable c# c# list objects to datatable how to datatable to list in c# c# generic list to datatable convert list into datatable c# convert datatable to list object in c# how to convert a list to datatable in c# convert list to datatable c# datatable to object list convert list to datatable c# entity framework convert datatable to list in c# c# convert list to datatable c# object array to datatable datatable to list in c# convert datatable to listc# List to datatable c# short c# from datatable to list convert datatable to list c# how to convert list to datatable in c# convert list to datatable in c# list to datatable c# list convert to datatable in c# convert list to datatable c# c# datatable to listview how to convert datatable data into list of object in c# c# list to datatable
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source