DataTable对象系列化后可以用Linq获取值方法
2015-09-17 11:16
239 查看
系统化后可以用linq,不用循环语句,要方便很多,获取值方法:
//类 public class User { public int ID { get; set; } public string Name { get; set; } } //对象转换 private static List<T> TableToEntity<T>(DataTable dt) where T : class,new() { Type type = typeof(T); List<T> list = new List<T>(); foreach (DataRow row in dt.Rows) { PropertyInfo[] pArray = type.GetProperties(); T entity = new T(); foreach (PropertyInfo p in pArray) { if (row[p.Name] is Int64) { p.SetValue(entity, Convert.ToInt32(row[p.Name]), null); continue; } p.SetValue(entity, row[p.Name], null); } list.Add(entity); } return list; } // 调用: List<User> userList = TableToEntity<User>(YourDataTable); --获取table内的一字段值 int count1=dtOrderDetails.AsEnumerable().Where(t => t.Field<string>("SKU").Substring(0, 2) == "MH" || t.Field<string>("SKU").Substring(0, 2) == "MK").Count() int count2=dtOrderDetails.AsEnumerable().Where(e => e.Field<decimal>("UnitPrice") == 0 || e.Field<decimal>("SalesPrice") == 0).Count() int count3=dtOrderDetails.AsEnumerable().Where(e => e.Field<int>("BType") == 1 && e.Field<string>("BValue") == MemberID.ToString()).Count()
相关文章推荐
- Notepad++ 更换主题
- IOS 调用摄像头拍照
- Android SDK 完整版
- ZygoteInit 相关分析
- java类型转换:String类型的数字转换成固定格式类型输出的String(有无规则均可)
- Android上下文菜单用法实例分析
- django 相关问题集锦
- Android listview addHeaderView 和 addFooterView 详解 不显示问题
- 网络通信——libevent安装,sample编译,sample运行
- VirtualBox 4.1安裝Ubuntu 12.04
- linux_vim_最佳快捷键
- CentOS查看系统连接状态
- iOS开发:MRC和ARC
- github分支创建与切换
- 20150917
- Bitcode
- Android LayoutInflater
- W5100的逆袭
- why docker?
- fedora 安装快盘