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;
        }
  • 相关阅读:
    查找数据库表中重复的 Image 类型值
    C#中的引用传递和值传递。
    用JS解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
    根据业务自己设计的.NET工厂模式架构
    封装EF code first用存储过程的分页方法
    2013款MacBook Air装Windows7单系统
    js判断是否在微信浏览器中打开
    EF Code First连接现有数据库
    JS中for循序中延迟加载实现动态效果
    DIV+CSS左右两列自适应高度的方法
  • 原文地址:https://www.cnblogs.com/smallfa/p/1609855.html
Copyright © 2011-2022 走看看