LINQ到实体选择查询错误
本文关键字:查询 错误 选择 实体 LINQ | 更新日期: 2023-09-27 17:52:39
using (SmartEntities employeeverificationcontext = new SmartEntities())
{
Employee emp = (from c in employeeverificationcontext.EmployeeEntity
where c.Employee_ID == emp_detail.EmployeeID
select new Employee {c.Status}).FirstOrDefault();
emp.Status = emp_detail.Status;
int i=employeeverificationcontext.SaveChanges();
if (i > 0)
{
result = "Verification Process Completed";
}
}
error: error 1 Cannot initialize type 'SmartWCF。因为它没有实现'System.Collections.IEnumerable'**
应该选择当前对象(c
)而不是select new Employee {c.Status}
所以你的查询应该是:
Employee emp = (from c in employeeverificationcontext.EmployeeEntity
where c.Employee_ID == emp_detail.EmployeeID
select c).FirstOrDefault();
或
Employee emp = employeeverificationcontext.EmployeeEntity
.FirstOrDefault(c=> Employee_ID == emp_detail.EmployeeID);
只选择Status
是这样做的:
var employeeStatus = (from c in employeeverificationcontext.EmployeeEntity
where c.Employee_ID == emp_detail.EmployeeID
select c.Status).FirstOrDefault();
但是,如果您想将其设置为新状态并将其保存到数据上下文中,这将无法帮助您。在这种情况下,您必须选择Employee
。
这篇文章中提到了一些替代方案:如何在LINQ中更新单列而不加载整行?