public void ExpressionParameterRemapperLinqToObjectsTest1() { //which id's to fetch const int FirstExpressionIdToFetch = 1; const int SecondExpressionIdToFetch = 2; //let's create 2 expressions Expression<Func<DummyObject, bool>> Expression1 = x => x.Id == FirstExpressionIdToFetch; //create the 2nd expression Expression<Func<DummyObject, bool>> Expression2 = x => x.Id == SecondExpressionIdToFetch; //we are going to merge the 2nd expression into the first one...the main goal of the expression visitor is to reset the ParameterExpression so x => is the same x var SecondExpressionRemappedParameters = new ExpressionParameterRemapper(Expression1.Parameters, Expression2.Parameters).Visit(Expression2.Body); //so now we should be able to combine the expressions var CombinedExpression = Expression.OrElse(Expression1.Body, SecondExpressionRemappedParameters); //i should be able to run this now Func<DummyObject, bool> CombinedExpressionInFunc = Expression.Lambda<Func<DummyObject, bool>>(CombinedExpression, Expression1.Parameters).Compile(); //let's go run the function var ResultsOfLinqToObject = DummyObject.CreateDummyListLazy(5).Where(CombinedExpressionInFunc).ToArray(); //check the results now Assert.Equal(2, ResultsOfLinqToObject.Length); //make sure we have the first item Assert.True(ResultsOfLinqToObject.Any(x => x.Id == FirstExpressionIdToFetch)); //make sure we have the 2nd item Assert.True(ResultsOfLinqToObject.Any(x => x.Id == SecondExpressionIdToFetch)); }
public void ExpressionParameterRemapperEntityFrameworkTest1() { DataProviderSetupTearDown.TearDownAndBuildUpDbEnvironment(); using (var DP = DIUnitTestContainer.DIContainer.Resolve<EntityFrameworkDP<EntityFrameworkEntityDP>>(EntityFrameworkTest.ReadonlyDataProviderName)) { //which id's to fetch const int FirstExpressionIdToFetch = 1; const int SecondExpressionIdToFetch = 2; //let's create 2 expressions Expression<Func<Ref_Test, bool>> Expression1 = x => x.Id == FirstExpressionIdToFetch; //create the 2nd expression Expression<Func<Ref_Test, bool>> Expression2 = x => x.Id == SecondExpressionIdToFetch; //we are going to merge the 2nd expression into the first one...the main goal of the expression visitor is to reset the ParameterExpression so x => is the same x var SecondExpressionRemappedParameters = new ExpressionParameterRemapper(Expression1.Parameters, Expression2.Parameters).Visit(Expression2.Body); //so now we should be able to combine the expressions var CombinedExpression = Expression.OrElse(Expression1.Body, SecondExpressionRemappedParameters); //i should be able to run this now var CombinedExpressionInFunc = Expression.Lambda<Func<Ref_Test, bool>>(CombinedExpression, Expression1.Parameters); //let's go run the function var ResultsOfLinqToObject = DP.Fetch<Ref_Test>(false).Where(CombinedExpressionInFunc).ToArray(); //check the results now Assert.Equal(2, ResultsOfLinqToObject.Length); //make sure we have the first item Assert.True(ResultsOfLinqToObject.Any(x => x.Id == FirstExpressionIdToFetch)); //make sure we have the 2nd item Assert.True(ResultsOfLinqToObject.Any(x => x.Id == SecondExpressionIdToFetch)); } }