[英]convert generic list to datatable using linq..?
List<Employee> objEmpList = new List<Employee>();
for (int i = 0; i < 5; i++)
{
objEmpList.Add(new Employee(){ID=i, Name="aa" + i});
}
I want to create table with generic list items as rows using linq or lambda expressions...How to do that..??I did that using loops but I want do that using linq..! 我想使用linq或lambda表达式创建具有通用列表项的表作为行...如何做到?? ??我使用循环做了但我想用linq做那个..!
Use CopyToDataTable extension. 使用CopyToDataTable扩展名。 See msdn article How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
请参阅msdn文章如何:实现通用类型T不是DataRow的CopyToDataTable
DataTable table = objEmpList.CopyToDataTable();
BTW here is simpler way to create list of employees: 顺便说一句,这里是创建员工列表的简单方法:
Enumerable.Range(0,5)
.Select(i => new Employee { ID = i, Name = "aa" + i })
.ToList();
Consider also using NBuilder : 考虑也使用NBuilder :
Builder<Employee>.CreateListOfSize(5).Build()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.