0
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";
    }
}

错误:错误 1 ​​无法使用集合初始化程序初始化类型“SmartWCF.Employee”,因为它没有实现“System.Collections.IEnumerable”**

4

2 回答 2

1

而不是select new Employee {c.Status}您应该选择当前对象 ( c)

所以你的查询应该是:

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);
于 2013-01-11T08:01:51.243 回答
0

只有选择Status是这样完成的:

var employeeStatus = (from c in employeeverificationcontext.EmployeeEntity 
                        where c.Employee_ID == emp_detail.EmployeeID 
                      select c.Status).FirstOrDefault();

但是,如果您想将其设置为新状态并将其保存到您的数据上下文中,这对您没有帮助。在这种情况下,您必须选择Employee

这篇文章中提到了一些替代方案:如何在不加载整行的情况下更新 LINQ 中的单个列?

于 2013-01-11T08:16:00.510 回答