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;
}

几个类和Table的方法的更多相关文章

  1. python 类、对象、方法、属性

    在python中,一个对象的特征也称为属性(attribute).它所具有的行为也称为方法(method) 结论:对象=属性+方法 在python中,把具有相同属性和方法的对象归为一个类(class) ...

  2. python中的类,对象,方法,属性等介绍

    注:这篇文章写得很好.加底纹的是我自己的理解 python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性( ...

  3. python类、对象、方法、属性之类与对象笔记

    python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性(特征)和行为来描述一个对象的.比如家里的小狗, ...

  4. Bootstrap Table使用方法详解

    http://www.jb51.net/article/89573.htm bootstrap-table使用总结 bootstrap-table是在bootstrap-table的基础上写出来的,专 ...

  5. Atitti 载入类的几种方法    Class.forName ClassLoader.loadClass  直接new

    Atitti 载入类的几种方法    Class.forName ClassLoader.loadClass  直接new 1.1. 载入类的几种方法    Class.forName ClassLo ...

  6. java进阶之反射:反射基础之如何获取一个类以及如何获取这个类的所有属性和方法(2)

    当我们知道一个类的对象,或者知道一个类的路径,或者指导这个类的名称的时候我们可以获取到这个类的类对象 当我们仅仅知道一个类的类对象的时候我们依然无法操作这个类,因为我们不知道这个类的属性,类的方法.那 ...

  7. JQuery操作类数组的工具方法

    JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...

  8. Python - 类与对象的方法

    类与对象的方法

  9. Java中是否可以调用一个类中的main方法?

    前几天面试的时候,被问到在Java中是否可以调用一个类中的main方法?回来测试了下,答案是可以!代码如下: main1中调用main2的主方法 package org.fiu.test; impor ...

随机推荐

  1. stack与heap、new的内存分配、static对象。(effective c++ 04)

    阅读effective c++ 04 (30页) 提到的static对象和堆与栈对象."不同编译单元内定义的non-local static对象". 了解一下.    目录 sta ...

  2. centOS下lnamp安装

    首先安装apache,mysql ,最后安装php 1>apache安装 安装:yum install -y httpd 运行:/bin/systemctl start httpd.servic ...

  3. H5 移动APP - 面包店

    使用jquerymobile.html5.css3实现移动APP 流程图 打包 用hbuilder实现打包 效果截图展示 图2-1 APP图标 图2-2 主页 图2-3 面包列表&详细信息 图 ...

  4. python3 完全平方数(循环)

    题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 代码: for i in range(1,85): if 168 % i == 0: j = 168 ...

  5. 谈谈你对Hibernate的理解

    答: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管程序运行的过程和操作怎么样,本质上都是要得到一 ...

  6. Python学习笔记:面向对象(类)

    1.类定义:Python3中,如果新建的类没有继承任何其他类,默认继承基础类object.Python2中如果没有显式继承object类就是经典类,而显式继承了object类就是新式类,Python2 ...

  7. STM32CUBEMX入门学习笔记2:关于STM32芯片使用内部flash

    找到正点原子的官网,下载他的HAL库:http://www.openedv.com/thread-109778-1-1.html 找到此例程,并打开其工程文件. 找到此文件,复制到自己工程里 复制到自 ...

  8. stm32之ADC应用实例(单通道、多通道、基于DMA)

    文本仅做记录.. 硬件:STM32F103VCT6 开发工具:Keil uVision4 下载调试工具:ARM仿真器 网上资料很多,这里做一个详细的整合.(也不是很详细,但很通俗).  所用的芯片内嵌 ...

  9. poj-3009 curling2.0(搜索)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26408   Accepted: 10546 Des ...

  10. Cplex: MIP Control Callback

    *本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 之前,我们有简单提到Cplex中的MIP Callback Interface,包括了Informational callback, q ...