zoukankan      html  css  js  c++  java
  • 几个类和Table的方法

    public class TableHelper
        {
            public static DataTable CreateTableFromClass(Type t)
            {
                DataTable dt = new DataTable();
                PropertyInfo[] pis = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                int colNum = t.GetProperties().Count();
                for (int c = 0; c < colNum; c++)
                {
                    dt.Columns.Add(pis[c].Name, typeof(string));
                }
                return dt;
            }
    
            public static DataTable ConvertListToTable<T>(List<T> stores, DataTable dt) where T : class
            {
                Type objType = typeof(T);
                int colNum = objType.GetProperties().Count();
                DataColumnCollection cols = dt.Columns;
    
                if (stores.Count > 0)
                {
                    for (int r = 0; r < stores.Count; r++)
                    {
                        var store = stores[r];
                        DataRow dr = dt.NewRow();
    
                        for (int c = 0; c < colNum; c++)
                        {
                            PropertyInfo pi = objType.GetProperty(cols[c].ColumnName, BindingFlags.Public | BindingFlags.Instance);
                            object value = pi.GetValue(store, null);
                            dr[c] = value;
                        }
                        dt.Rows.Add(dr);
                    }
                }
                return dt;
            }
        }
    View Code
    使用linq to DataTable group by实现
    var query = from t in dt.AsEnumerable()
                group t by new { t1 = t.Field<string>("name"), t2 = t.Field<string>("sex") } into m
                select new
                {
                    name = m.Key.t1,
                    sex = m.Key.t2,
                    score = m.Sum(n => n.Field<decimal>("score"))
                };
    if (query.ToList().Count > 0)
    {
        query.ToList().ForEach(q =>
        {
            Console.WriteLine(q.name + "," + q.sex + "," + q.score);
        });
    }  
    View Code
    public static T PostDataToModel(HttpContext context,T model)
            {
                Type t = model.GetType();
                PropertyInfo[] pis = t.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (!string.IsNullOrEmpty(context.Request.Form[pi.Name]))
                    {
                        pi.SetValue(model, Convert.ChangeType(context.Request.Form[pi.Name], pi.PropertyType));
                    }
                }
                return model;
            }
    View Code
            public DataTable ConvertListToTable<T>(List<T> stores) where T : class
            {
                Type objType = typeof(T);
                DataTable dt = CreateTableFromClass(objType);
    
                int colNum = objType.GetProperties().Count();
                DataColumnCollection cols = dt.Columns;
    
                if (stores.Count > 0)
                {
                    for (int r = 0; r < stores.Count; r++)
                    {
                        var store = stores[r];
                        DataRow dr = dt.NewRow();
    
                        for (int c = 0; c < colNum; c++)
                        {
                            PropertyInfo pi = objType.GetProperty(cols[c].ColumnName, BindingFlags.Public | BindingFlags.Instance);
                            object value = pi.GetValue(store, null);
                            dr[c] = value;
                        }
                        dt.Rows.Add(dr);
                    }
                }
                return dt;
            }
    View Code

    出处: http://www.cnblogs.com/windy2008

  • 相关阅读:
    递归的效率问题以及递归与循环的比较
    malloc/free与new/delete的区别
    类的综合案例——纯虚函数与抽象类( 加强对接口与多态,以及派生类构造函数的理解 )
    对象移动、右值引用详解
    深入理解类成员函数的调用规则(理解成员函数的内存为什么不会反映在sizeof运算符上、类的静态绑定与动态绑定、虚函数表)
    为什么NULL指针也能访问成员函数?(但不能访问成员变量)
    洛谷P1656 炸铁路
    Vijos1901 学姐的钱包
    洛谷P2327 [SCOI2005] 扫雷
    洛谷P1993 小 K 的农场
  • 原文地址:https://www.cnblogs.com/windy2008/p/4730628.html
Copyright © 2011-2022 走看看