zoukankan      html  css  js  c++  java
  • IQueryable 转化为DataTable

    1:要属性是否可以为空System.Nullable`1[]

    2:string  属性不为空

    public static class ExtendIQueryable

    {
       
        public static DataTable ConvertToDataTable(System.Linq.IQueryable o)
        {
            DataTable  dt= new DataTable();
            var props = o.ElementType.GetProperties();
            foreach (System.Reflection.PropertyInfo pi in props)
            {

                string ddd = pi.PropertyType.ToString();

        

                if ((pi.PropertyType == System.Type.GetType("System.Nullable`1[System.Int32]")) || (pi.PropertyType == System.Type.GetType("System.Int32")))
                {
                    dt.Columns.Add(pi.Name, System.Type.GetType("System.Int32"));
                }
                else if (pi.PropertyType == System.Type.GetType("System.String"))
                {
                    dt.Columns.Add(pi.Name, System.Type.GetType("System.String"));
                }
                else if ((pi.PropertyType == System.Type.GetType("System.Nullable`1[System.DateTime]")) ||  (pi.PropertyType == System.Type.GetType("System.DateTime")))
                {
                    dt.Columns.Add(pi.Name, System.Type.GetType("System.DateTime"));
                }
                else
                {
                    dt.Columns.Add(pi.Name, System.Type.GetType("System.Decimal"));
                }
                
            }
            foreach (var item in o)
            {
                DataRow dr = dt.NewRow();
                foreach (System.Reflection.PropertyInfo pi in props)
                {
                    if (pi.GetValue(item,null) != null)
                    {
                        if (pi.GetValue(item,null) != null)
                        {
                            dr[pi.Name] = pi.GetValue(item,null);
                        }
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
  • 相关阅读:
    “非工作总结”之快门—我的镜头见过你
    书摘:日本式管理和依靠自己
    寒冬日,找阳光
    模式自由(Schemafree)和数据存储的非格式化趋势
    心体澄澈,意气和平
    思考些管理的事情
    含沙射影,业镜照胆
    临崖之马,上滩之舟—凡事一定要区别不同情况对待
    [转]HttpContext.Current.Cache 和 HttpRuntime.Cache
    句柄、引用、指针与对象(转)
  • 原文地址:https://www.cnblogs.com/smallfa/p/1609855.html
Copyright © 2011-2022 走看看