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;
        }
  • 相关阅读:
    安信天行全方位信息安全态势感知平台建设与运营
    SQL基础总结——20150730
    中兴推“小兴看看”,诠释智能家电的真谛
    Java 线程第三版 第九章 Thread调度 读书笔记
    3930: [CQOI2015]选数|递推|数论
    S​D​I​与​A​S​I 接口具体解释介绍
    通过双重for循环来找到JSON中不反复的数据
    蓝桥杯 2016/3/17 測试 前6题题解...
    [疯狂Java]JDBC:事务管理、中间点、批量更新
    Linux下搭建Memcached缓存系统
  • 原文地址:https://www.cnblogs.com/smallfa/p/1609855.html
Copyright © 2011-2022 走看看