Pushkar Jethwa
Pushkar Jethwa

Reputation: 43

LINQ to Entity Select Query Error

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'**

Upvotes: 0

Views: 670

Answers (2)

Jacco
Jacco

Reputation: 3271

Only selecting the Status is done like this:

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

But if you want to set it to a new status and save that to your data context, this won't help you. You do have to select the Employee in that case.

There are a few alternatives mentioned in this post: How to update a single column in LINQ without loading the entire row?

Upvotes: 0

Habib
Habib

Reputation: 223187

Instead of select new Employee {c.Status} you should select the current object (c)

So your query should be:

Employee emp = (from c in employeeverificationcontext.EmployeeEntity 
               where c.Employee_ID == emp_detail.EmployeeID 
               select c).FirstOrDefault();

or

Employee emp = employeeverificationcontext.EmployeeEntity
                      .FirstOrDefault(c=> Employee_ID == emp_detail.EmployeeID);

Upvotes: 1

Related Questions