转换方法代码:

 private DataTable ConvertToTable(IQueryable query)
        {
            DataTable dtList 
= new DataTable();
            
bool isAdd = false;
            PropertyInfo[] objProterties 
= null;
            
foreach (var item in query)
            {
                
if (!isAdd)
                {
                    objProterties 
= item.GetType().GetProperties();
                    
foreach (var itemProterty in objProterties)
                    {
                        Type type 
= null;
                        
if (itemProterty.PropertyType != typeof(string&& itemProterty.PropertyType != typeof(int&& itemProterty.PropertyType != typeof(DateTime))
                        {
                            type 
= typeof(string);
                        }
                        
else
                        {
                            type 
= itemProterty.PropertyType;
                        }
                        dtList.Columns.Add(itemProterty.Name, type);
                    }
                    isAdd 
= true;
                }
                var row 
= dtList.NewRow();
                
foreach (var pi in objProterties)
                {
                    row[pi.Name] 
= pi.GetValue(item, null);
                }
                dtList.Rows.Add(row);
            }

            
return dtList;
        }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
猜你喜欢
  • 2022-01-23
  • 2022-12-23
  • 2021-08-19
  • 2021-11-07
  • 2021-08-07
相关资源
相似解决方案