开发者

Generic Edit in repository

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 propert开发者_StackOverflow社区ies)
    {
        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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜