/*********************************************************************** * Project: baifenBinfa * ProjectName: 百分兵法管理系统 * Web: http://chuanyin.com * Author: * Email: * CreateTime: 202403/02 * Description: 暂无 ***********************************************************************/ using System; using System.Linq; using System.Linq.Expressions; namespace CoreCms.Net.Model.Entities.Expression { public static class PredicateBuilder { public static Expression> True() { return f => true; } public static Expression> False() { return f => false; } public static Expression Compose(this Expression first, Expression second, Func merge) { // build parameter map (from parameters of second to parameters of first) var map = first.Parameters.Select((f, i) => new {f, s = second.Parameters[i]}) .ToDictionary(p => p.s, p => p.f); // replace parameters in the second lambda expression with parameters from the first var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); // apply composition of lambda expression bodies to parameters from the first expression return System.Linq.Expressions.Expression.Lambda(merge(first.Body, secondBody), first.Parameters); } /// /// 扩展啦 /// /// /// /// /// public static Expression> And(this Expression> first, Expression> second) { return first.Compose(second, System.Linq.Expressions.Expression.AndAlso); } public static Expression> Or(this Expression> first, Expression> second) { return first.Compose(second, System.Linq.Expressions.Expression.OrElse); } } }