我想创建扩展方法
static IUpdatable<T> Set<T>(this IUpdatable<T> list, T item) where T : class
将崩溃:
db.Orders
.Where(o => o.OrderId == order.OrderId)
.Where(o => o.RowVersion == order.RowVersion)
.Set(o => o.AvgDeliveryTime, order.AvgDeliveryTime)
.Set(o => o.AvgSellerTime, order.AvgSellerTime)
.Set(o => o.BuyerId, order.BuyerId)
// There are other properties
.Set(o => o.ProductName, order.ProductName)
.Set(o => o.QuantityLot, order.QuantityLot)
.Set(o => o.SellerOrderId, order.SellerOrderId)
.Update();
到此:
db.Orders
.Where(o => o.OrderId == order.OrderId)
.Where(o => o.RowVersion == order.RowVersion)
.Set(o => o, order)
.Update();
它应该能够为每个属性添加Set-method。 如何实现这种方法?
UPD: 像这样:
public static IUpdatable<T> Set<T>(this IUpdatable<T> list, T item) where T : class
{
PropertyInfo[] props = typeof (T).GetProperties();
var result = list;
foreach (PropertyInfo prop in props)
{
result = result.Set(i=>prop.GetSetMethod(false), prop.GetValue(item, prop.Name) ); // This is wrong
}
return result;
}
UPD2: Set-method的重载:
public static IUpdatable<T> Set<T, TV>(this IQueryable<T> source, Expression<Func<T, TV>> extract, Expression<Func<T, TV>> update);
public static IUpdatable<T> Set<T, TV>(this IQueryable<T> source, Expression<Func<T, TV>> extract, Expression<Func<TV>> update);
public static IUpdatable<T> Set<T, TV>(this IQueryable<T> source, Expression<Func<T, TV>> extract, TV value);
public static IUpdatable<T> Set<T, TV>(this IUpdatable<T> source, Expression<Func<T, TV>> extract, Expression<Func<T, TV>> update);
public static IUpdatable<T> Set<T, TV>(this IUpdatable<T> source, Expression<Func<T, TV>> extract, Expression<Func<TV>> update);
public static IUpdatable<T> Set<T, TV>(this IUpdatable<T> source, Expression<Func<T, TV>> extract, TV value);