I have a Update
method like this:
public void Update(MyClass item, System.Linq.Expressions.Expression<Func<MyClass, bool>> exp)
and I update status field like this:
MyClass u = ent.MyClass.Where(exp).FirstOrDefault();
if (u == null)
{
throw new Exception("No Record Found");
}
else
{
u.Status=item.Status; <-------
ent.SaveChanges();
}
ok ,the problem is I want to use this update method for various updates for example user may want to update status,Name
or Tel,fax,Address,name
and ...
I want to check I property isn't null it assign to similar property of selected object(in line that show with arrow). How I can to this automatically ? I don't want wtite like this:
if(item.Status != null)
{
u.Status = item.Status;
}
if(item.Name != null)
{
u.Name = item.Name;
}
,....
thanks
You can
use reflection
to check for null. Only overhead would be to pass thepropertyName
explicitly to your method -