问题
public T Update(T update) { }
Currently for this method I m implementing it like so..is there any other easy way around in Linq to SQL
public T Update (T update)
{
var table = (IEnumerable<T>)(_table.AsQueryable());
var store = (T)((from a in table.Where(
a => a.GetType().GetPrimaryKey() ==
update.GetType().GetPrimaryKey()) select a).Single());
PropertyInfo[] properties = store.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
Object propertyValue = null;
if (null != property.GetSetMethod())
{
PropertyInfo entityProperty =
update.GetType().GetProperty(property.Name);
if (entityProperty.PropertyType.BaseType ==
Type.GetType("System.ValueType") ||
entityProperty.PropertyType ==
Type.GetType("System.String"))
propertyValue =
update.GetType().GetProperty(property.Name).GetValue(update, null);
if (null != propertyValue)
property.SetValue(store, propertyValue, null);
}
}
_context.submitChanges
return update
}
This works fine, however I was hoping if I could improve this code please...
_table is an ITable
来源:https://stackoverflow.com/questions/3923196/generic-edit-in-repository