2013-01-11 193 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