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;
        }
  • 相关阅读:
    第 17 章 责任链模式【Chain of Responsibility Pattern】
    第 16 章 观察者模式【Observer Pattern】
    第 15 章 组合模式【Composite Pattern】
    第 14 章 迭代器模式【Iterator Pattern】
    第 13 章 装饰模式【Decorator Pattern】
    第 12 章 命令模式【Command Pattern】
    第 11 章 桥梁模式【Bridge Pattern】
    第 10 章 建造者模式【Builder Pattern】
    编写高质量代码改善C#程序的157个建议——导航开篇
    C#实现简易ajax调用后台方法
  • 原文地址:https://www.cnblogs.com/smallfa/p/1609855.html
Copyright © 2011-2022 走看看