关键词:
在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有list<T>检索方便。但是数据以ILIST形式返回,就为我们在.NET中使用传统的数据绑定造成了不便。下面介绍datatable和list的相互转换
1.datatable转换为list
public List<T> GetList<T>(DataTable table) { List<T> list = new List<T>(); T t = default(T); PropertyInfo[] propertypes = null; string tempName = string.Empty; foreach (DataRow row in table.Rows) { t = Activator.CreateInstance<T>(); propertypes = t.GetType().GetProperties(); foreach (PropertyInfo pro in propertypes) { tempName = pro.Name; if (table.Columns.Contains(tempName)) { object value = row[tempName]; if (!value.ToString().Equals("")) { pro.SetValue(t, value, null); } } } list.Add(t); } return list.Count == 0 ? null : list; }
2.list转换为datatable
public DataSet ConvertToDataSet<T>(IList<T> list) { if (list == null || list.Count <= 0) { return null; } DataSet ds = new DataSet(); DataTable dt = new DataTable(typeof(T).Name); DataColumn column; DataRow row; System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (T t in list) { if (t == null) { continue; } row = dt.NewRow(); for (int i = 0, j = myPropertyInfo.Length; i < j; i++) { System.Reflection.PropertyInfo pi = myPropertyInfo[i]; string name = pi.Name; if (dt.Columns[name] == null) { column = new DataColumn(name, pi.PropertyType); dt.Columns.Add(column); } row[name] = pi.GetValue(t, null); } dt.Rows.Add(row); } ds.Tables.Add(dt); return ds; }
封装一个list集合和datatable相互转换的工具类(代码片段)
///<summary>///List转换为DataTable对象///</summary>publicclassListTranTableModel///<summary>///新增的列名称///</summary>publicstringaddColumNameget;set;///<summary>///新增列的默认信息/// 查看详情
asp.net中datatable和list的优劣处
...用list<model>l来保存数据,一般用sqldatareader读取数据转换成实体后存入list编写是好写更多代码,如直接用datable保存数据代码更简单,但操作list比data灵活。不知在性能上市list<model>好还是datable的好,希望能详细指出list和... 查看详情
将datatable转换为list,将list转换为datatable的实现类
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;namespaceXmh.DBUnit{///< 查看详情
c#datatable转list和list转datatable
1、///将List<>转换为DataTablepublicstaticDataTableListToDataTable<T>(IEnumerable<T>collection)varprops=typeof(T).GetProperties();vardt=newDataTable();dt.Columns.AddRange(props.Sel 查看详情
list,泛型和datatable的相互转换
publicstaticDataTableToDataTableTow(IListlist){DataTableresult=newDataTable();if(list.Count>0){PropertyInfo[]propertys=list[0].GetType().GetProperties();foreach(PropertyInfopiinpropertys){result.Co 查看详情
如何用 List(Of t) 填充 DataTable 或将 List(Of t) 转换为 DataTable?
】如何用List(Oft)填充DataTable或将List(Oft)转换为DataTable?【英文标题】:HowtofillaDataTablewithaList(Oft)orconvertaList(Oft)toaDataTable?【发布时间】:2009-11-2621:04:46【问题描述】:我已经阅读了很多关于这个主题的帖子;其中包括最近的.NET-Co... 查看详情
如何将DataTable的列转换为List
】如何将DataTable的列转换为List【英文标题】:HowtoconvertacolumnofDataTabletoaList【发布时间】:2011-09-3020:40:08【问题描述】:我有一个包含多列的DataTable。我想从DataTable的第一列中得到一个List<String>。我该怎么做?【问题... 查看详情
c#将datatable转换成list的方法
本文实例讲述了C#将DataTable转换成list及数据分页的方法。分享给大家供大家参考。具体如下:///<summary> ///酒店评论列表-分页 ///</summary> ///<paramname="userId"></param> ///<paramname="pageIndex 查看详情
将 DataTable 转换为 List<T>
】将DataTable转换为List<T>【英文标题】:ConvertDataTabletoList<T>【发布时间】:2010-11-2811:42:56【问题描述】:我有一个MyType类型的强类型数据表,我想将其转换为List<MyType>。我该怎么做?谢谢。【问题讨论】:DataT... 查看详情
将list转换为datatable的方法
publicstaticDataTableCopyToDataTable<T>(thisIEnumerable<T>array){varret=newDataTable();foreach(PropertyDescriptordpinTypeDescriptor.GetProperties(typeof(T)))ret.Columns.Add(dp.Name);foreac 查看详情
list转换datatable
///<summary> ///将泛类型集合List类转换成DataTable ///</summary> ///<paramname="list">泛类型集合</param> ///<returns></returns> publicSystem.Data.DataTab 查看详情
datatable转换为list
///<summary>///利用反射将DataTable转换为List<T>对象///</summary>///<paramname="dt">DataTable对象</param>///<returns>List<T>集合</returns>publicstaticList<T>Data 查看详情
datatable转换成list
usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Reflection;usingSystem.Text; publicclassHelper<T>whereT:new(){///<summary>///datatable转list///</summa 查看详情
list泛型转换成datatable
publicDataTableListToDataTable<T>(List<T>list){DataTabledt=newDataTable("CarInfo");Typetype=typeof(T);//根据类型获取类型System.Reflection.PropertyInfo[]properts=type.GetProperties();//根据反射类型获取类型所有 查看详情
datatable转换为其他对象
1.将DataTable类型的数据转换成List<T>集合1///<summary>2///将DataTable类型的数据转换成List<T>集合T实体3///</summary>4///<typeparamname="T"></typeparam>5///<paramname="dataTable"></p 查看详情
泛型list转换成datatable(代码片段)
1privateDataTablelistToDataTable<T>(List<T>ListItem)23//实列化DataTable对象4vardt=newDataTable(typeof(T).Name);56//通过反射获取List实体的属性信息7PropertyInfo[]propertyInfos=typeof(T).GetProperties(BindingFlags.Public|BindingFlags.Instance);89//给DataTable添加定义1... 查看详情
datatable转换为list
publicList<string[]>SetList(DataTabledt) { List<string[]>list=newList<str 查看详情
将 DataTable 转换为 List<T>
】将DataTable转换为List<T>【英文标题】:ConvertDataTabletoList<T>【发布时间】:2011-01-1721:54:57【问题描述】:我有一个疑问,有时我从DataTable转换为List<T>:List<EDog>lstDogs=(fromdrRowindsDogs.Tables[0].AsEnumerable()selectnew... 查看详情