Skip to content
Loading
  • Sivakumar Koneti
    0
    Hello Reylin Mathew
     
    Follow the below you can get exact solution
     
    https://stackoverflow.com/questions/18608959/convert-linq-query-results-to-datatable-c-sharp
    0
  • Veerendra Annigere
    0
    Hi

    1. public static DataTable ConvertToDataTable(this IEnumerable dataList) where T:class  
    2.       {  
    3.           DataTable convertedTable = new DataTable();  
    4.           PropertyInfo[] propertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);  
    5.           foreach(PropertyInfo prop in propertyInfo)  
    6.           {  
    7.               convertedTable.Columns.Add(prop.Name);  
    8.           }  
    9.   
    10.           foreach(T item in dataList)  
    11.           {  
    12.               var row = convertedTable.NewRow();  
    13.               var values = new object[propertyInfo.Length];  
    14.               for(int i=0;i
    15.               {  
    16.                   var test = propertyInfo[i].GetValue(item, null);  
    17.                   row[i] = propertyInfo[i].GetValue(item,null);  
    18.               }  
    19.               convertedTable.Rows.Add(row);  
    20.           }  
    21.           return convertedTable;  
    22.       }  
    Usage:
     
    1. DataTable listTable = YourList.ConvertToDataTable();  
    0
  • Jignesh Kumar
    0
    Hi Reylin,
     
    Please try this link which will help you,
    https://www.c-sharpcorner.com/blogs/converting-generic-list-into-datatable 
    0