public IQueryable <SourceNode> GetQueryable(Expression <Func <SourceNode, bool> > condition, int startIndex = 0, int pageSize = 0) { Expression <Func <SourceNode, bool> > where1 = x => (!x.IsDeleted); var where = SwapVisitor.MergeWithAnd <SourceNode>(condition, where1); // Expression<Func<Country, Client, object>> where = (x, y) => x.Name == y.Name; return(_ModelRepository.GetQueryable(where, x => x.Id, startIndex, pageSize)); }
public override Expression <Func <TEntity, bool> > ToExpression() { var leftExpression = this.left.ToExpression(); var rightExpression = this.right.ToExpression(); var expression = new SwapVisitor(leftExpression.Parameters[0], rightExpression.Parameters[0]).Visit(leftExpression.Body); var conditionExpression = this.conditionalFunc(expression, rightExpression.Body); return(Expression.Lambda <Func <TEntity, bool> >(conditionExpression, rightExpression.Parameters)); }
public static ISpecification <T> Or <T>(this ISpecification <T> left, Expression <Func <T, bool> > rightExpression) where T : class//, IEntity { Expression <Func <T, bool> > leftExpression = left.Criteria; var visitor = new SwapVisitor(leftExpression.Parameters[0], rightExpression.Parameters[0]); BinaryExpression lazyOr = Expression.OrElse(visitor.Visit(leftExpression.Body) !, rightExpression.Body); var or = Expression.Lambda <Func <T, bool> >(lazyOr, rightExpression.Parameters); return(new Specification <T>(or)); }
public IQueryable <Channels> GetQueryable(Expression <Func <Channels, bool> > condition, int startIndex = 0, int pageSize = 0) { Expression <Func <Channels, bool> > where1 = x => !x.IsDeleted; var where = SwapVisitor.MergeWithAnd <Channels>(condition, where1); // Expression<Func<Country, Client, object>> where = (x, y) => x.Name == y.Name; var list = _ModelRepository.GetQueryable(where, x => x.Id, startIndex, pageSize); return(list); }
public static IQueryable <Foo> WhereTransactionDate( this IQueryable <Foo> source, Expression <Func <DateTime, bool> > predicate) { var visited = SwapVisitor.Swap(predicate.Body, predicate.Parameters.Single(), tranDateTime.Body); var lambda = Expression.Lambda <Func <Foo, bool> >( visited, tranDateTime.Parameters); return(source.Where(lambda)); }
static void Main() { Expression <Func <Source, IEnumerable <Value> > > f1 = source => source.Values; Expression <Func <IEnumerable <Value>, bool> > f2 = vals => vals.Any(v => v.FinalValue == 3); // change the p0 from f2 => f1 var body = SwapVisitor.Swap(f2.Body, f2.Parameters[0], f1.Body); var lambda = Expression.Lambda <Func <Source, bool> >(body, f1.Parameters); // which is: // source => source.Values.Any(v => (v.FinalValue == 3)) }
public static IQueryable <TSource> Search <TSource>(this IQueryable <TSource> source, string searchTerm, params Expression <Func <TSource, string> >[] stringProperties) { if (String.IsNullOrEmpty(searchTerm)) { return(source); } if (stringProperties.Length == 0) { return(source.Where(x => false)); } // The lamda I would like to reproduce: // source.Where(x => x.[property1].Contains(searchTerm) // || x.[property2].Contains(searchTerm) // || x.[property3].Contains(searchTerm)...) //Create expression to represent x.[property1].Contains(searchTerm) var searchTermExpression = Expression.Constant(searchTerm); var param = stringProperties[0].Parameters.Single(); Expression orExpression = null; //Build a contains expression for each property foreach (var stringProperty in stringProperties) { // re-write the property using the param we want to keep var body = SwapVisitor.Swap(stringProperty.Body, stringProperty.Parameters.Single(), param); var checkContainsExpression = Expression.Call( body, typeof(string).GetMethod("Contains"), searchTermExpression); if (orExpression == null) { orExpression = checkContainsExpression; } else { // compose orExpression = Expression.OrElse(orExpression, checkContainsExpression); } } var lambda = Expression.Lambda <Func <TSource, bool> >(orExpression, param); return(source.Where(lambda)); }
public static Expression <Func <TValue, bool> > Combine <TValue>( this Expression <Func <TValue, bool> > left, Expression <Func <TValue, bool> > right, Func <Expression, Expression, BinaryExpression> combination) { if (left == null) { left = value => true; } // rewrite the body of "right" using "left"'s parameter in place // of the original "right"'s parameter var newRight = new SwapVisitor(right.Parameters[0], left.Parameters[0]) .Visit(right.Body); // combine via && / || etc and create a new lambda return(Expression.Lambda <Func <TValue, bool> >( combination(left.Body, newRight), left.Parameters)); }
private static Expression <Func <TEntity, bool> > And(Expression <Func <TEntity, bool> > left, Expression <Func <TEntity, bool> > right) { if (left == null) { throw new ArgumentNullException(nameof(left)); } if (right == null) { throw new ArgumentNullException(nameof(right)); } var visitor = new SwapVisitor(left.Parameters[0], right.Parameters[0]); var binaryExpression = Expression.AndAlso(visitor.Visit(left.Body), right.Body); var lambda = Expression.Lambda <Func <TEntity, bool> >(binaryExpression, right.Parameters); return(lambda); }
public static Expression <T> CombineExpressions <T>(IList <Expression <T> > expressions) { var length = expressions.Count; if (length == 0) { return(null); } var combination = expressions[0]; for (var i = 1; i < length; i++) { var expression = expressions[i]; var visitor = new SwapVisitor(combination.Parameters[0], expression.Parameters[0]); combination = Expression.Lambda <T>(Expression.AndAlso(visitor.Visit(combination.Body), expression.Body), expression.Parameters); } return(combination); }
static void Main() { Expression <Func <Model, Foo> > expression1 = m => m.SubModel.Foo; Expression <Func <Foo, string> > expression2 = f => f.Bar.Value; var swap = new SwapVisitor(expression2.Parameters[0], expression1.Body); var lambda = Expression.Lambda <Func <Model, string> >( swap.Visit(expression2.Body), expression1.Parameters); // test it worked var func = lambda.Compile(); Model test = new Model { SubModel = new SubModel { Foo = new Foo { Bar = new Bar { Value = "abc" } } } }; Console.WriteLine(func(test)); // "abc" }
static TExpr SubstituteIn <TExpr>(TExpr expression, Expression orig, Expression replacement) where TExpr : Expression { var replacer = new SwapVisitor(orig, replacement); return(replacer.VisitAndConvert(expression, "ReplaceExpressions")); }
// from http://stackoverflow.com/a/33442945/369247 public static Expression <Func <TFrom, TTo> > Chain <TFrom, TMiddle, TTo>(this Expression <Func <TFrom, TMiddle> > first, Expression <Func <TMiddle, TTo> > second) { Expression visitedSecond = new SwapVisitor(second.Parameters[0], first.Body).Visit(second.Body); return(Expression.Lambda <Func <TFrom, TTo> >(visitedSecond, first.Parameters)); }
// weakly-typed version of the above method public static LambdaExpression Chain(this LambdaExpression first, LambdaExpression second) { Expression visitedSecond = new SwapVisitor(second.Parameters[0], first.Body).Visit(second.Body); return(Expression.Lambda(visitedSecond, first.Parameters)); }