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;
        }
  • 相关阅读:
    ASP.NET Cache的一些总结分享
    C#中委托和事件的区别实例解析
    [hdu2544]最短路spfa
    [codeforces274b]Zero Tree(树形dp)
    [poj2151]Check the difficulty of problems概率dp
    [poj3071]football概率dp
    [poj3744]Scout YYF I(概率dp+矩阵快速幂)
    [bzoj2440]完全平方数(二分+mobius反演)
    [xdoj1216]子树第k小(dfs序+主席树)
    [xdoj1233]Glory and LCS
  • 原文地址:https://www.cnblogs.com/smallfa/p/1609855.html
Copyright © 2011-2022 走看看