private static Expression CheckNullOnEnumerableParent(ExpressionParserPieceGroup group, ExpressionParserPieceGroup parent, Expression parentExpression, bool shouldSkipLast) { string path = null; if (shouldSkipLast) { path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(t => t.Name)); } else { path = string.Join(".", group.Pieces.Select(t => t.Name)); } if (!string.IsNullOrEmpty(path)) { var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path, ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, parameter: group.Parameter, nullChecking: true); //public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); parentExpression = Expression.Call(typeof(Enumerable), "Where", new Type[] { parent.GroupEnumerableType() }, parentExpression, whereExpression); } return(parentExpression); }
private Expression CheckNullOnFirstGroup(ExpressionParserPieceGroup group, Expression currentExpression) { var path = string.Join(".", group.Pieces.Select(t => t.Name)); var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path, ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, parameter: group.Parameter, nullChecking: true); var whereBodyExpression = (whereExpression as LambdaExpression).Body; whereBodyExpression = Expression.Not(whereBodyExpression); var nullType = currentExpression.Type; Expression ifTrueExpression = null; if (QueryableHelpers.IsGenericEnumerable(nullType)) { var listType = typeof(List <>).MakeGenericType(nullType.GenericTypeArguments.First()); ifTrueExpression = Expression.New(listType); } else { ifTrueExpression = Expression.Default(nullType); } return(Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type)); }
public Task <TEntity> GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken)) { var keyProp = GetBsonIdProperty(); var expression = QueryableHelpers.CreateConditionExpression <TEntity>(keyProp.Name, DynamicLinq.ConditionOperators.Equal, key, DynamicLinq.QueryConvertStrategy.LeaveAsIs); var result = Collection.Find(expression).FirstOrDefaultAsync(); return(result); }
protected virtual Expression <Func <TEntity, bool> > CreateEntityExpression(TEntity entity) { var objectKey = GetBsonIdProperty(); var constant = objectKey.GetValue(entity); var expression = QueryableHelpers.CreateConditionExpression <TEntity>(objectKey.Name, DynamicLinq.ConditionOperators.Equal, constant, DynamicLinq.QueryConvertStrategy.LeaveAsIs); return(expression); }
protected virtual Expression BuildConditionExpression(ParameterExpression parameter, WhereBuilderCondition filter) { var ret = QueryableHelpers.CreateConditionExpression( parameter.Type, filter.Path, filter.ConditionOperator, filter.Value, filter.ConvertStrategy, filter.CollectionHandling, parameter: parameter, nullChecking: IsNullCheckingEnabled, stringComparision: filter.StringComparisation ); return(ret); }
public void TestCreateFilterExpression() { var authors = new List <Author>() { new Author { Id = 1, FirstName = "David", LastName = "Lebee", Posts = new List <Post> { new Post { Id = 1, AuthorId = 1, Title = "Match", Content = "ABC", Comments = new List <Comment>() { new Comment() { Id = 1, DisplayName = "John Doe", CommentText = "!@#$!@#!@#", Email = "*****@*****.**" } } }, new Post { Id = 2, AuthorId = 1, Title = "Match", Content = "ABC", Comments = new List <Comment>() } } }, new Author { Id = 2, FirstName = "Chuck", LastName = "Norris", Posts = new List <Post> { new Post { Id = 3, AuthorId = 2, Title = "Match", Content = "ASD", Comments = new List <Comment>() }, new Post { Id = 4, AuthorId = 2, Title = "DontMatch", Content = "ASD", Comments = new List <Comment>() } } } }; // the query. var query = authors.AsQueryable(); var allExpression = QueryableHelpers.CreateConditionExpression <Author>("Posts.Title", ConditionOperators.Equal, "Match", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.All); var anyExpression = QueryableHelpers.CreateConditionExpression <Author>("Posts.Title", ConditionOperators.Equal, "Match", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.Any); var anyExpression2 = QueryableHelpers.CreateConditionExpression <Author>("Posts.Comments.Email", ConditionOperators.Equal, "*****@*****.**", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.Any); Assert.AreEqual(1, query.Count(allExpression)); Assert.AreEqual(2, query.Count(anyExpression)); Assert.AreEqual(1, query.Count(anyExpression2)); }