This question already has answers here:
How do you convert a DataTable into a generic list?
(23个答案)
3年前关闭。
在给定的代码中,我编写了lambda表达式,但是它显示了一个错误-无法将lambda表达式转换为类型'string',因为它不是委托类型。
(23个答案)
3年前关闭。
在给定的代码中,我编写了lambda表达式,但是它显示了一个错误-无法将lambda表达式转换为类型'string',因为它不是委托类型。
EmployeeDataOperation emp = new EmployeeDataOperation(); //Class to perform CRUD operation.
List<EmployeeProp> data = new List<EmployeeProp>();
dt = emp.getEmployeeData();//return datatable with records.
//I want to use lambda expression to use Datatable data as a list
data = (from a in dt
select new EmployeeProp { Name = a.Name, Email = a.Email }).ToList();
//error near select
return View(data);
最佳答案
您可以使用.Field
linq扩展名或索引/列名(例如,row [“ columnname”]从DataRow
访问值。我建议使用Field
扩展名,因为它甚至可以处理nullable
类型。
data = dt.AsEnumerable()
.Select(row=> new EmployeeProp ()
{
Name = row.Field<string>("Name"),
Email = row.Field<string>("Email ")
// Other properties....
})
.ToList();
关于c# - 如何使用Lambda表达式获取DataTable记录到列表中? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37457611/