开发者

Generic Edit in repository

开发者 https://www.devze.com 2023-01-20 00:02 出处:网络
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) { }

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

精彩评论

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