2013-01-11 56 views
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"; 
    } 
} 

error: Error 1 Cannot initialize type 'SmartWCF.Employee' with a collection initializer because it does not implement 'System.Collections.IEnumerable'**LINQ到实体选择查询错误

回答

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); 
+0

我这样做,但它选择所有我只是想选择状态列列... Thnks帮助表示赞赏。 –

+0

@PushkarJethwa,您只能选择状态栏,用''没有新Employee'选择c.status'。您无法投射到实体框架中的实体类型。但是选择状态并不能帮助你在后面的代码中做什么。您将无法设置'emp.Status = emp_detail.Status' – Habib

0

只有选择Status像这样做:

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

但是,如果你想将它设置为一个新的状态并保存到您的数据上下文,这不会帮助你。在这种情况下,您必须选择Employee

有在这篇文章中提到的几个备选方案:How to update a single column in LINQ without loading the entire row?

+0

感谢reply..it意味着没有办法选择员工EMP只有特定的列; –

+0

您可以轻松选择一个列,并将其更新,使其更复杂一些。我在回答中添加了另一篇文章的链接。 – Jacco