如何组合多个 c# Lambda 表达式 (Expression<Func<T, T>>)
How to combine multiple c# Lambda Expressions (Expression<Func<T, T>>)
我有以下功能,它实际上是 Z.EntityFramework.Plus 批量更新的包装器:
public static int UpdateBulk<T>(this IQueryable<T> query, Expression<Func<T, T>> updateFactory) where T : IBaseEntity, new()
{
Expression<Func<T, T>> modifiedExpression = x => new T() { ModifiedBy = "Test", ModifiedDate = DateTime.Now };
var combine = Expression.Lambda<Func<T, T>>(
Expression.AndAlso(
Expression.Invoke(updateFactory, updateFactory.Parameters),
Expression.Invoke(modifiedExpression, modifiedExpression.Parameters)
),
updateFactory.Parameters.Concat(modifiedExpression.Parameters)
); //This returns an error
return query.Update(combine);
}
这样调用:
decimal probId = ProbId.ParseDecimal();
db.Problems
.Where(e => e.ProbId == probId)
.UpdateBulk(e => new Problem() {
CatId = Category.ParseNullableInt(),
SubCatId = SubCategory.ParseNullableInt(),
ListId = Problem.ParseNullableInt()
});
其中IBaseEntity定义如下:
public abstract class IBaseEntity
{
public System.DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedBy { get; set; }
public string DeletedBy { get; set; }
}
'Problem'class顺便实现了IBaseEntity。
我想做的是自动将 ModifiedBy 和 ModifiedDate 附加到 UpdateBulk 函数中的 updateFactory,这样就不必在每次调用 UpdateBulk 时都这样做。
我尝试在上面的 UpdateBulk 函数中将解析的 'updateFactory' 表达式与 'modifiedExpression' 结合起来,但是 returns 错误:
the binary operator AndAlso is not defined for the types 'Problem'
是否可以像这样合并表达式?如果可以,我做错了什么?
如果没有,如何将 ModifiedBy = "Test"、ModifiedDate = DateTime.Now 合并到 updateFactory 表达式中?
谢谢,
杆
您不能使用 AndAlso
,因为那是针对 BinaryExpression - Expression<Func<T,bool>>
的,在这种情况下,您需要 Expression Visitor,定义为 here by Marc Gravell(所以他值得所有功劳)
我使用相同的方法为您的情况提供解决方案,假设 Problem class schema
,粘贴 Linqpad 代码:
void Main()
{
var final = UpdateBulk((Problem p) => new Problem{CatId = 1,SubCatId = 2, ListId=3});
// final is of type Expression<Func<T,T>>, which can be used for further processing
final.Dump();
}
public static Expression<Func<T, T>> UpdateBulk<T>(Expression<Func<T, T>> updateFactory) where T : IBaseEntity, new()
{
Expression<Func<T, T>> modifiedExpression = x => new T() { ModifiedBy = "Test", ModifiedDate = DateTime.Now };
var result = Combine(updateFactory, modifiedExpression);
return result;
}
static Expression<Func<TSource, TDestination>> Combine<TSource, TDestination>(
params Expression<Func<TSource, TDestination>>[] selectors)
{
var param = Expression.Parameter(typeof(TSource), "x");
return Expression.Lambda<Func<TSource, TDestination>>(
Expression.MemberInit(
Expression.New(typeof(TDestination).GetConstructor(Type.EmptyTypes)),
from selector in selectors
let replace = new ParameterReplaceVisitor(
selector.Parameters[0], param)
from binding in ((MemberInitExpression)selector.Body).Bindings
.OfType<MemberAssignment>()
select Expression.Bind(binding.Member,
replace.VisitAndConvert(binding.Expression, "Combine")))
, param);
}
class ParameterReplaceVisitor : ExpressionVisitor
{
private readonly ParameterExpression from, to;
public ParameterReplaceVisitor(ParameterExpression from, ParameterExpression to)
{
this.from = from;
this.to = to;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return node == from ? to : base.VisitParameter(node);
}
}
public abstract class IBaseEntity
{
public System.DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedBy { get; set; }
public string DeletedBy { get; set; }
}
public class Problem : IBaseEntity
{
public int CatId { get; set; }
public int SubCatId { get; set; }
public int ListId { get; set; }
}
我有以下功能,它实际上是 Z.EntityFramework.Plus 批量更新的包装器:
public static int UpdateBulk<T>(this IQueryable<T> query, Expression<Func<T, T>> updateFactory) where T : IBaseEntity, new()
{
Expression<Func<T, T>> modifiedExpression = x => new T() { ModifiedBy = "Test", ModifiedDate = DateTime.Now };
var combine = Expression.Lambda<Func<T, T>>(
Expression.AndAlso(
Expression.Invoke(updateFactory, updateFactory.Parameters),
Expression.Invoke(modifiedExpression, modifiedExpression.Parameters)
),
updateFactory.Parameters.Concat(modifiedExpression.Parameters)
); //This returns an error
return query.Update(combine);
}
这样调用:
decimal probId = ProbId.ParseDecimal();
db.Problems
.Where(e => e.ProbId == probId)
.UpdateBulk(e => new Problem() {
CatId = Category.ParseNullableInt(),
SubCatId = SubCategory.ParseNullableInt(),
ListId = Problem.ParseNullableInt()
});
其中IBaseEntity定义如下:
public abstract class IBaseEntity
{
public System.DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedBy { get; set; }
public string DeletedBy { get; set; }
}
'Problem'class顺便实现了IBaseEntity。
我想做的是自动将 ModifiedBy 和 ModifiedDate 附加到 UpdateBulk 函数中的 updateFactory,这样就不必在每次调用 UpdateBulk 时都这样做。
我尝试在上面的 UpdateBulk 函数中将解析的 'updateFactory' 表达式与 'modifiedExpression' 结合起来,但是 returns 错误:
the binary operator AndAlso is not defined for the types 'Problem'
是否可以像这样合并表达式?如果可以,我做错了什么? 如果没有,如何将 ModifiedBy = "Test"、ModifiedDate = DateTime.Now 合并到 updateFactory 表达式中?
谢谢, 杆
您不能使用 AndAlso
,因为那是针对 BinaryExpression - Expression<Func<T,bool>>
的,在这种情况下,您需要 Expression Visitor,定义为 here by Marc Gravell(所以他值得所有功劳)
我使用相同的方法为您的情况提供解决方案,假设 Problem class schema
,粘贴 Linqpad 代码:
void Main()
{
var final = UpdateBulk((Problem p) => new Problem{CatId = 1,SubCatId = 2, ListId=3});
// final is of type Expression<Func<T,T>>, which can be used for further processing
final.Dump();
}
public static Expression<Func<T, T>> UpdateBulk<T>(Expression<Func<T, T>> updateFactory) where T : IBaseEntity, new()
{
Expression<Func<T, T>> modifiedExpression = x => new T() { ModifiedBy = "Test", ModifiedDate = DateTime.Now };
var result = Combine(updateFactory, modifiedExpression);
return result;
}
static Expression<Func<TSource, TDestination>> Combine<TSource, TDestination>(
params Expression<Func<TSource, TDestination>>[] selectors)
{
var param = Expression.Parameter(typeof(TSource), "x");
return Expression.Lambda<Func<TSource, TDestination>>(
Expression.MemberInit(
Expression.New(typeof(TDestination).GetConstructor(Type.EmptyTypes)),
from selector in selectors
let replace = new ParameterReplaceVisitor(
selector.Parameters[0], param)
from binding in ((MemberInitExpression)selector.Body).Bindings
.OfType<MemberAssignment>()
select Expression.Bind(binding.Member,
replace.VisitAndConvert(binding.Expression, "Combine")))
, param);
}
class ParameterReplaceVisitor : ExpressionVisitor
{
private readonly ParameterExpression from, to;
public ParameterReplaceVisitor(ParameterExpression from, ParameterExpression to)
{
this.from = from;
this.to = to;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return node == from ? to : base.VisitParameter(node);
}
}
public abstract class IBaseEntity
{
public System.DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedBy { get; set; }
public string DeletedBy { get; set; }
}
public class Problem : IBaseEntity
{
public int CatId { get; set; }
public int SubCatId { get; set; }
public int ListId { get; set; }
}