泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Wolfy.List2DataTable
{
class Program
{
static void Main(string[] args)
{
List<Person> lst = new List<Person>()
{
, Gender=false, Name="wolfy1"},
, Gender=false, Name="wolfy2"},
, Gender=false, Name="wolfy3"},
, Gender=false, Name="wolfy4"},
, Gender=false, Name="wolfy5"},
};
DataTable dt = List2DataTable<Person>(lst);
Console.WriteLine("转换结束");
Console.Read();
}
/// <summary>
/// 将泛型集合转换为datatable
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="entities"></param>
/// <returns></returns>
static DataTable List2DataTable<TEntity>(List<TEntity> entities)
{
if (entities == null)
{
throw new ArgumentNullException("转换的集合为空");
}
Type type = typeof(TEntity);
PropertyInfo[] properties = type.GetProperties();
DataTable dt = new DataTable(type.Name);
foreach (var item in properties)
{
dt.Columns.Add(new DataColumn(item.Name) { DataType = item.PropertyType });
}
foreach (var item in entities)
{
DataRow row = dt.NewRow();
foreach (var property in properties)
{
row[property.Name] = property.GetValue(item);
}
dt.Rows.Add(row);
}
return dt;
}
}
public class Person
{
public int ID { set; get; }
public string Name { set; get; }
public bool Gender { set; get; }
}
}
泛型集合转换为DataTable的更多相关文章
- [工具类]泛型集合转换为DataTable
写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...
- C#基础知识之泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- 泛型集合、datatable常用数据类型转换Json帮助类
泛型集合.datatable常用数据类型转换Json帮助类 using System; using System.Data; using System.Configuration; using Sys ...
- linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)
在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...
- 使用泛型集合取代datatable作为返回值实现面向对象
开会的时候,师父说.我们在机房重构时,尽量不要用datatable作为返回值.改用泛型集合的方式,这样能够实现真正的面向对象. 通过查资料和同学交流,把这个问题给攻克了. 对于泛型集合.我也有了一些认 ...
- C#;DataTable添加列;DataTable转List泛型集合;List泛型集合转DataTable泛型集合;
给DataTable添加列 string sql = "select * from cgpmb order by code"; DataTable dt = Bobole.Data ...
- c#将list集合转换为datatable的简单办法
public static class ExtensionMethods { /// <summary> /// 将List转换成DataTabl ...
- C# 集合转换为DataTable
该类就用了几个类型,如int,int?,string,所以其它类型就先没管. 用到的类: public class tb_Projects { public int ProID { get; set; ...
- List泛型集合转DataTable
自存,此方法可以防止出现DataSet不支持System.Nullable的错误 public static DataTable ToDataTable<T>(IEnumerable< ...
随机推荐
- [Java] 读写字节数据,过滤流DataOutputStream和DataInputStream
package test.stream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io ...
- Jetty提交数据时报java.lang.IllegalStateException: Form too large270468>200000问题解决
今天在使用Eclipse的Jetty插件做为服务器提交富文本编辑中的数据时,报如下异常: 在\eclipse\plugins目录下,找到org.mortbay.jetty.server_6.1.23. ...
- js中格式化时间字符串
.net 程序员肯定有遇到过,将一个对象json序列化之后Date 字段 就会转化成 '/Date(1370770323740)/' 这种格式的数据,下面介绍一种在js中,关于时间格式的转换. < ...
- [ASP.NET]动态绑定树控件:
public void BindTree(TreeView tview, TreeNode tn_main, string parentId,string sql) { TreeNode tn=nul ...
- python3 AttributeError: 'NoneType' object has no attribute 'split'
from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...
- Android——TabWidget
1.activity_tabwidget.xml <?xml version="1.0" encoding="utf-8"?><TabHost ...
- 03-position和anchorPoint
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- appium的xpath定位
做自动化,元素定位是我们遇到的第一个困难.总是会有各种各样的问题,导致我们定位不到元素.前面一篇博客也写了元素定位的几种方法,今天主要分享一下xpath的定位方法. 这里我们仍然拿计算器举例. 比如我 ...
- 软件测试—— junit 单元测试
Tasks: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma with Eclipse Write a java pro ...
- (转)C#进行图像处理的几种方法(Bitmap,BitmapData,IntPtr)
转自 http://blog.sina.com.cn/s/blog_628821950100wh9w.html C#进行图像处理的几种方法 本文讨论了C#图像处理中Bitmap类.BitmapData ...