Ank*_*odi 1 c# model-view-controller asp.net-mvc
在给定的代码中,我编写了 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);
Run Code Online (Sandbox Code Playgroud)
您可以使用.Fieldlinq 扩展或索引/列名(例如 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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9937 次 |
| 最近记录: |