DataTable转化为List
public List<T> ConvertToList<T>(DataTable dt) where T : new()
{
// 定义集合
List<T> ts = new List<T>();
// 获得此模型的类型
Type type = typeof(T);
string tempName = "";
// 获得此模型的公共属性
PropertyInfo[] propertys = type.GetProperties();
T t = new T();
foreach (DataRow dr in dt.Rows)
{
t = new T();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
// 检查DataTable是否包含此列
if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue;
if (dr[tempName] != DBNull.Value)
pi.SetValue(t, dr[tempName], null);
}
}
ts.Add(t);
}
return ts;
}
调用时:
ConvertToList<TreeNodeInt>(ds.Tables[0])
另附上 生成树形结构的简便方法,此处没有使用递归是他的高明之处,可以借鉴。
public class TreeNodeInt
{
public int id { get; set; }
public int? parentId { get; set; }
public string title { get; set; }
public string key { get; set; }
public string longLabel { get; set; }
public bool isFolder { get; set; }
public bool isLazy { get; set; }
public bool expand { get; set; }
//public bool Select { get; set; }
public bool unselectable { get; set; }
public bool hideCheckbox { get; set; }
public int treeLevel { get; set; }
public object obj { get; set; }
public List<TreeNodeInt> children { get; set; }
public static List<TreeNodeInt> ConvertToTree(List<TreeNodeInt> itemList)
{
itemList = itemList.OrderBy(p => p.title).ToList();
var tree = (from i in itemList
where i.parentId == null || i.parentId == 0
//orderby i.title
select new TreeNodeInt
{
id = i.id,
key = i.key,
//key = i.id.ToString(),
title = i.title,
longLabel = i.longLabel,
isFolder = i.isFolder,
expand = i.expand,
isLazy = i.isLazy,
//Select = i.Select,
unselectable = i.unselectable,
hideCheckbox = i.hideCheckbox,
treeLevel = i.treeLevel,
obj = i.obj
}).ToList();
Queue<TreeNodeInt> queue = new Queue<TreeNodeInt>(tree);
while (queue.Count > 0)
{
var node = queue.Dequeue();
var children = (from i in itemList
where i.parentId == node.id && i.id != node.id
//let idStr = SqlFunctions.StringConvert((double)i.id).Trim()
select new TreeNodeInt
{
id = i.id,
parentId = i.parentId,
key = i.key,
//key = i.id.ToString(),
title = i.title,
longLabel = i.longLabel,
isFolder = i.isFolder,
expand = i.expand,
isLazy = i.isLazy,
//Select = i.Select,
unselectable = i.unselectable,
hideCheckbox = i.hideCheckbox,
treeLevel = i.treeLevel,
obj = i.obj
}).ToList();
if (children.Count > 0)
{
node.children = children;
foreach (var child in children)
{
queue.Enqueue(child);
}
}
}
return tree;
}
}
DataTable转化为List的更多相关文章
- C#将DataTable转化为List<T>
C#将DataTable转化为List<T> 在使用三层架构开发一个网站时,希望把DataTable对象转换为List<T>对象,于是在网上找资料,总结一个比较方便的方法来实现 ...
- LINQ查询返回DataTable类型[轉]與将DataTable序列化为Json格式【轉】
(原文地址:http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/) LINQ查询返回DataTable类型 在使 ...
- C#中DataTable转化为List<T>解析
在.net项目中使用到DataTable和List<T>集合的地方较多, 泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性.不会强行对值类型进行装箱和拆箱,或对引用类 ...
- DataTable转化为Model
/// <summary> /// 将DataTable转成Model /// </summary> /// <param name="dt"> ...
- 将DataTable格式化为json字符串返回
一般用于ajax局部刷新的使用比较多,通过查询得到了DataTable数据,要想将数据放回需要将DataTable转换为json格式,以下为转换的调用函数: string json = "& ...
- .NET DataTable转化为json格式
标准的json用“分隔,不用' public static string DataSetToJson(DataTable dt) { string json = string.Empty ...
- 将DataTable转化为json对象
private string DataTableTojson(DataTable dt) { List> list=new List>(); ...
- 将SqlDataReader 数据集转化为datatbale ,在将datatable 转化为iList
public IList GetModelList(string tablename, string where) { IList list = null; DataTable dataTable = ...
- DataTable 转换成 Json的3种方法
在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...
随机推荐
- AIX 7.1 install python
周围环境AIX7.1 设备python-2.6.2 因为互联网是非常多的安装文档.而且也没有细挑的版本号.因为我觉得python2.6 可能相对保守一些,至少之前用到的版本号是这个.所以此处依旧 ...
- js操作string它substr方法
substr 方法 从指定的指定起始位置的长度返回一个子. stringvar.substr(start [, length ]) 參数 stringvar 必选项. 要提取子字符串的字符串文字或 S ...
- js匀速运动停止条件
匀速运动,怎么让它到达指定位置时停止呢? 原理: 1,物体和目标的差值距离小于等于速度时,即停止 2,接着让物体移动位置等于目标位置 示例:匀速运动停止 html部分 <input type=& ...
- 使用 WPF 创建单实例应用程序
一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...
- SRM 628 D1L3:DoraemonPuzzleGame,math,后市展望,dp
称号:c=problem_statement&pm=13283&rd=16009">http://community.topcoder.com/stat?c=probl ...
- RSA算法加密解密
该工具类中用到了BASE64,需要借助第三方类库:javabase64-1.3.1. jar 注意:RSA加密明文最大长度117字节,解密要求密文最大长度为128字节,所以在加密和解密的过程中需要分块 ...
- Web Deploy发布网站及常见问题解决方法(图文)
Web Deploy发布网站及常见问题解决方法(图文) Windows2008R2+IIs7.5 +Web Deploy 3.5 Web Deploy 3.5下载安装 http://www.iis.n ...
- 部署Redis主-从
Redis主-从部署实践 0. 前言 这篇文章简要介绍Redis的主从部署,实现了一主二从,使用两个哨兵监控,以实现简单的HA,其中从库作为备机. 1. 部署 这里有三台服务器,其中239主机上的Re ...
- github jekyll site不再使用Maruku由于Markdown翻译员,但kramdown
今天写了一篇博客,之push至jekyll site on github在,发现总是错的,例如,下面的电子邮件消息: The page build completed successfully, bu ...
- 使用 gridfs-stream 存储文件遇到的一个坑。
前一段时间参读了某个coder写的用 gridfs-stream 来存储文件,感觉不错就自己用 gridfs-stream 模块写了一个文件存储服务,但是发现存储的文件总是删不掉, 我调用的是GFS的 ...