Rod*_*Rod 5 c# linq expression entity-framework-plus
我有以下函数,它实际上是 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);
}
Run Code Online (Sandbox Code Playgroud)
像这样调用:
decimal probId = ProbId.ParseDecimal();
db.Problems
.Where(e => e.ProbId == probId)
.UpdateBulk(e => new Problem() {
CatId = Category.ParseNullableInt(),
SubCatId = SubCategory.ParseNullableInt(),
ListId = Problem.ParseNullableInt()
});
Run Code Online (Sandbox Code Playgroud)
其中 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; }
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,“问题”类实现了 IBaseEntity。
我想要做的是自动将 ModifiedBy 和 ModifiedDate 附加到 UpdateBulk 函数中的 updateFactory,这样就不必在每次调用 UpdateBulk 时都这样做。
我尝试在上面的 UpdateBulk 函数中将解析的“updateFactory”表达式与“modifiedExpression”结合起来,但它返回错误:
没有为“问题”类型定义二元运算符 AndAlso
是否可以像这样合并表达式,如果可以,我做错了什么?如果没有,我如何将 ModifiedBy = "Test", ModifiedDate = DateTime.Now 合并到 updateFactory 表达式中?
谢谢,罗德
您不能使用AndAlso,因为这意味着BinaryExpression - Expression<Func<T,bool>>,在这种情况下您需要表达式访问者,如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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4046 次 |
| 最近记录: |