tags:

views:

163

answers:

1

this is the way i used to save record with linq: (my Q is below)

    public void SaveEmployee(Employee employee)
    {

        using (BizNetDB db = new BizNetDB())
        {
            BizNet.SqlRep.Data.Employee oldEmployee = (from e in db.Employees
                                                       where e.EmployeeID == employee.EmployeeID
                                                   select e).SingleOrDefault();
            if (oldEmployee == null)
            {
                oldEmployee = new BizNet.SqlRep.Data.Employee();
                oldEmployee.BirthDate = employee.BirthDate;
                oldEmployee.WorkRole = employee.WorkRole;
                oldEmployee.CurrentFlag = employee.CurrentFlag;
                oldEmployee.HireDate = employee.HireDate;
                ...
                db.Employees.InsertOnSubmit(oldEmployee);
            }
            else
            {
                if (oldEmployee.BirthDate.Date != employee.BirthDate.Date)
                    oldEmployee.BirthDate = employee.BirthDate;
                if (oldEmployee.CurrentFlag != employee.CurrentFlag)
                    oldEmployee.CurrentFlag = employee.CurrentFlag;
                if (oldEmployee.HireDate.Date != employee.HireDate.Date)
                    oldEmployee.HireDate = employee.HireDate;
            }

            oldEmployee.ModifiedDate = DateTime.Now;
            db.SubmitChanges();
            employee.EmployeeID = oldEmployee.EmployeeID;
        }
    }

my questions are:

a. are the if statements nesccery? why not to make the assigning without the check? mybe the if block take more cpu..

b. why to spearate the new record block and the update block?

when the record is new it will do

db.Employees.InsertOnSubmit(oldEmployee);

and then proceed with the update routine...

+3  A: 

The way you're doing it the only reason you need the if statement is to new it up and insert it, so I would use the if statement just for that.

I would do this instead:

 public void SaveEmployee(Employee employee)
{
    using (BizNetDB db = new BizNetDB())
    {
        BizNet.SqlRep.Data.Employee oldEmployee = 
                    (from e in db.Employees
                    where e.EmployeeID == employee.EmployeeID
                    select e).SingleOrDefault();

        if (oldEmployee == null)
        {
            oldEmployee = new BizNet.SqlRep.Data.Employee();
            db.Employees.InsertOnSubmit(oldEmployee);
        }

        if (oldEmployee.BirthDate.Date != employee.BirthDate.Date)
            oldEmployee.BirthDate = employee.BirthDate;
        if (oldEmployee.CurrentFlag != employee.CurrentFlag)
            oldEmployee.CurrentFlag = employee.CurrentFlag;
        if (oldEmployee.HireDate.Date != employee.HireDate.Date)
            oldEmployee.HireDate = employee.HireDate;

        oldEmployee.ModifiedDate = DateTime.Now;
        db.SubmitChanges();
        employee.EmployeeID = oldEmployee.EmployeeID;
    }
}

I also think there's a way to map one object's properties to the other, but it escapes me at the moment. It may not work for what you're trying to do anyway since it seems that you're doing some other things later anyway with the ModifiedDate and EmployeeID.

Joseph
do you have any ex for the mapping?
ari