list互转datatable 支持Nullable转换
/// <summary>
/// list转datatable
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ListToTable(IList list)
{
DataTable dt = new DataTable();
if (list != null)
{
//通过反射获取list中的字段
System.Reflection.PropertyInfo[] p = list[].GetType().GetProperties();
foreach (System.Reflection.PropertyInfo pi in p)
{
var colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[];
} dt.Columns.Add(pi.Name, colType);
}
for (int i = ; i < list.Count; i++)
{
IList TempList = new ArrayList();
//将IList中的一条记录写入ArrayList
foreach (System.Reflection.PropertyInfo pi in p)
{
object oo = pi.GetValue(list[i], null);
TempList.Add(oo);
}
object[] itm = new object[p.Length];
for (int j = ; j < TempList.Count; j++)
{
itm.SetValue(TempList[j], j);
}
dt.LoadDataRow(itm, true);
}
}
return dt;
}
/// <summary>
/// DataTable转化为List集合
/// </summary>
/// <typeparam name="T">实体对象</typeparam>
/// <param name="dt">datatable表</param>
/// <param name="isStoreDB">是否存入数据库datetime字段,date字段没事,取出不用判断</param>
/// <returns>返回list集合</returns>
public static List<T> TableToList<T>(DataTable dt, bool isStoreDB = true)
{
List<T> list = new List<T>();
Type type = typeof(T);
List<string> listColums = new List<string>();
foreach (DataRow row in dt.Rows)
{
PropertyInfo[] pArray = type.GetProperties(); //集合属性数组
T entity = Activator.CreateInstance<T>(); //新建对象实例
foreach (PropertyInfo p in pArray)
{
if (!dt.Columns.Contains(p.Name) || row[p.Name] == null || row[p.Name] == DBNull.Value)
{
continue; //DataTable列中不存在集合属性或者字段内容为空则,跳出循环,进行下个循环
}
if (isStoreDB && p.PropertyType == typeof(DateTime) && Convert.ToDateTime(row[p.Name]) < Convert.ToDateTime("1753-01-01"))
{
continue;
}
try
{
var obj = Convert.ChangeType(row[p.Name], p.PropertyType);//类型强转,将table字段类型转为集合字段类型
p.SetValue(entity, obj, null);
}
catch (Exception)
{
// throw;
}
//if (row[p.Name].GetType() == p.PropertyType)
//{
// p.SetValue(entity, row[p.Name], null); //如果不考虑类型异常,foreach下面只要这一句就行
//}
//object obj = null;
//if (ConvertType(row[p.Name], p.PropertyType,isStoreDB, out obj))
//{
// p.SetValue(entity, obj, null);
//}
}
list.Add(entity);
}
return list;
}
list互转datatable 支持Nullable转换的更多相关文章
- 无法将类型“System.Nullable`1”强制转换为类型“System.Object”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型。
在一个项目中使用LINQ和EF时出现了题目所示的异常,搜索了很多资料都找不到解决办法,主要是因为EF方面的知识欠缺. 先将情况记录如下,以供以后参考. 查询主要设计两张表,由外键关联: 在进行下面的查 ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
- 一个高性能的对象属性复制类,支持不同类型对象间复制,支持Nullable<T>类型属性
由于在实际应用中,需要对大量的对象属性进行复制,原来的方法是通过反射实现,在量大了以后,反射的性能问题就凸显出来了,必须用Emit来实现. 搜了一圈代码,没发现适合的,要么只能在相同类型对象间复制,要 ...
- C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast(dynamic_cast还支持交叉转换,const_cast将一个类的const、volatile以及__unaligned属性去掉)
在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...
- 让.NET xml序列化支持Nullable
.NET的序列化,关于契约类的生成我们都是通过xsd.exe,对于值类型的可空判断是通过声明同名+Specified的bool属性来判断,比如: public class Person { publi ...
- 关于datatable linq的转换
关于datatable datarow DataTable paraval = GetParaVal(DateCondition, strUrl, Page, RowPage, iYearMonthN ...
- C#中DataTable与泛型集合互转(支持泛型集合中对象包含枚举)
最近在做WCF,因为是内部接口,很多地方直接用的弱类型返回(DataSet),这其实是一种非常不好的方式,最近将项目做了修改,将所有接口返回值都修改成强类型,这样可以减少很多与客户端开发人员的沟通,结 ...
- C# DataTable和List转换操作类
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...
- .NET(C#)中的DataSet、string、DataTable等对象转换成Json
ConvertJson.cs类 using System; using System.Collections.Generic; using System.Text; using System.Data ...
随机推荐
- ZOJ 1457 E-Prime Ring Problem
https://vjudge.net/contest/67836#problem/E A ring is compose of n circles as shown in diagram. Put n ...
- 图解linux安装tomcat(附常用命令)
本例使用的是centos6.5版本,具体内容如下 一.首先到官方下载tomcat服务 http://tomcat.apache.org/download-70.cgi 二.将tomcat上传至linu ...
- windows批处理学习(call与start)---02
参考:https://www.cnblogs.com/Braveliu/p/5078283.html 一.call命令总结 (1)call命令简介 语法: call [ [Drive:] [Path] ...
- android gradle打包常见问题及解决方案
背景: 问题: Q1: UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: Java heap space at com.android.d ...
- printf以及各种变种
int printf(char *format, [argument]); 其向终端(显示器.控制台等)输出字符 int fprintf(FILE*stream, const char*format, ...
- 单行文字溢出和多行文字溢出省略号显示的CSS样式
单行文字溢出,CSS样式 <h6 style="width:70px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis ...
- java得到当前时间
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date ...
- div、span绑定内容改变事件
内容改变事件onchange只适用于form表单标签(input.select.textarea) 当需要对div.span标签进行内容改变监听则无法适用,查阅了一些资料发现jquery有针对的方法, ...
- imshow(A,[])和imshow(A)的区别
imshow的用法: IMSHOW Display image. IMSHOW(I,N) displays the intensity image I with N discrete levels o ...
- RT-thread内核之消息队列
一.消息队列控制块:在include/rtdef.h中 #ifdef RT_USING_MESSAGEQUEUE /** * message queue structure */ struct rt_ ...