C#动态拼接lambda表达式
拼接帮助类
#region 动态拼接lambda表达式
public static class Utility
{
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.OrElse);
}
}
public class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}
protected override Expression VisitParameter(ParameterExpression p)
{
ParameterExpression replacement;
if (map.TryGetValue(p, out replacement))
{
p = replacement;
}
return base.VisitParameter(p);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
调用方法:
Expression<Func<OrderDO, bool>> expression=t=>true;
expression=expression.And(t => t.orderStartTime > start && t.orderStartTime < end);
if (!string.IsNullOrWhiteSpace(orderid))
{
expression= expression.And(t => t.OrderID == orderid);
}
if (!string.IsNullOrWhiteSpace(BatchID))
{
expression= expression.And(t => t.BatchNo == BatchID);
}
if(!string.IsNullOrWhiteSpace(deliver))
{
expression= expression.And(t => t.deliveryBillNo == deliver);
}
if(!string.IsNullOrWhiteSpace(UserTel))
{
expression= expression.And(t => t.buyerMobile == UserTel);
}
if(string.IsNullOrWhiteSpace(orderid)&&string.IsNullOrWhiteSpace(BatchID)&&string.IsNullOrWhiteSpace(deliver)&&string.IsNullOrWhiteSpace(UserTel))
{
expression= expression.And(t => (orderstatus == t.ApproveStatus || orderstatus == 0)&& (supplierid == t.supplierid || supplierid == 0)&& (ordertype == t.orderType || ordertype == 0));
}
orders= SqlsugarHelper.Init().GetClient().Queryable<OrderDO>().AS("Erp_Order_Info")
.Where(expression).OrderByDescending(item => item.orderStartTime)
.ToPageList(page, limit, ref totalcount);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27