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 = ; 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 > )
{
for (int r = ; r < stores.Count; r++)
{
var store = stores[r];
DataRow dr = dt.NewRow(); for (int c = ; 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;
}
}
使用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 > )
{
query.ToList().ForEach(q =>
{
Console.WriteLine(q.name + "," + q.sex + "," + q.score);
});
}
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;
}
        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 > )
{
for (int r = ; r < stores.Count; r++)
{
var store = stores[r];
DataRow dr = dt.NewRow(); for (int c = ; 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;
}

最新文章

  1. jQuery静态方法parseJSON方法使用和源码分析
  2. cell 内部 设置width 总不对
  3. java io流之字符流
  4. 配置org.springframework.scheduling.quartz.CronTriggerBean(转)
  5. 项目已被os x使用 不能打开-黑苹果之路
  6. UVa 10815 Andy&#39;s First Dictionary
  7. jQuery滚动监听插件Waypoints
  8. Jquer学习
  9. [LeetCode OJ] Sort Colors
  10. java中float和double的区别
  11. PowerShell: 问题,此系统上禁止运行脚本解决方法
  12. 安装yum源和gcc编译器遇到的问题
  13. SAP MM A工厂下的PR可以转成B工厂下的PO?
  14. Java关键字(四)——final
  15. CF739E Gosha is hunting
  16. 用pandas读取excel报错
  17. 2015-09-27 git学习
  18. CentOS7 安装java 环境 摘抄
  19. 宝塔面板快速开启https服务
  20. Spring 源码学习(4) —— 动态AOP使用示例

热门文章

  1. axure的基本使用方法(侧边导航栏的制作)
  2. javaweb基础(9)_Servlet生成验证码图片
  3. db2的定时备份
  4. Oracle11g 数据库的导入导出
  5. cocos2x (c++/lua) spine 文件的预加载
  6. jQuery plugin : bgStretcher 背景图片切换效果插件
  7. 大意了,这几道Python面试题没有答对,Python面试题No13
  8. Find a way HDU - 2612(bfs)
  9. jmeter中重要组件及其执行顺序
  10. luogu4016 负载平衡问题