public QuerySet <T> Where(T model) { ParameterExpression parameter = Expression.Parameter(typeof(T), "parameter"); var entityType = EntityCache.QueryEntity(typeof(T)); foreach (var item in entityType.Properties) { string name = item.Name; object value = item.GetValue(model, null); if (value != null) { if (item.PropertyType == typeof(int) || item.PropertyType == typeof(double) || item.PropertyType == typeof(decimal) || item.PropertyType == typeof(long)) { if (Convert.ToDecimal(value) == 0) { continue; } } else if (item.PropertyType == typeof(DateTime)) { var time = Convert.ToDateTime(value); if (time == DateTime.MinValue) { continue; } } var whereExpress = Expression.Equal(Expression.Property(parameter, name), Expression.Constant(value)); WhereExpressionList.Add(Expression.Lambda <Func <T, bool> >(TrimExpression.Trim(whereExpress), parameter)); } } return(this); }
/// <summary> /// 增加查询条件 /// </summary> /// <typeparam name="TResult">查询表达式的对象</typeparam> /// <param name="predicate">查询表达式</param> /// <returns></returns> public QueryContext <T> Where <TResult>(string alisName, Expression <Func <TResult, bool> > predicate) { ExpressionModel em = new ExpressionModel(); em.Body = predicate.Body; em.Model = typeof(TResult); em.UnionAlisName = alisName; WhereExpressionList.Add(em); return(this); }
public QuerySet <T> Where <TWhere1, TWhere2>(Expression <Func <TWhere1, TWhere2, bool> > exp) { //var sqlWhere = new WhereExpression(exp, $"Where_{Params.ParameterNames.Count()}_", SqlProvider); //WhereBuilder.Append(sqlWhere.SqlCmd); //if (sqlWhere.Param != null) //{ // Params.AddDynamicParams(sqlWhere.Param); //} WhereExpressionList.Add(exp); return(this); }
/// <summary> /// /// </summary> /// <param name="where"></param> /// <param name="truePredicate"></param> /// <param name="falsePredicate"></param> /// <returns></returns> public ICommand <T> WhereIf(bool where, Expression <Func <T, bool> > truePredicate, Expression <Func <T, bool> > falsePredicate) { if (where) { WhereExpressionList.Add(truePredicate); } else { WhereExpressionList.Add(falsePredicate); } return(this); }
/// <summary> /// 带前置条件的Where判断 /// </summary> /// <typeparam name="TWhere"></typeparam> /// <param name="where"></param> /// <param name="truePredicate"></param> /// <param name="falsePredicate"></param> /// <returns></returns> public IQuerySet <T> WhereIf <TWhere>(bool where, Expression <Func <TWhere, bool> > truePredicate, Expression <Func <TWhere, bool> > falsePredicate) { if (where) { WhereExpressionList.Add(truePredicate); } else { WhereExpressionList.Add(falsePredicate); } return(this); }
/// <summary> /// /// </summary> /// <param name="predicate"></param> /// <returns></returns> public ICommand <T> Where(Expression <Func <T, bool> > predicate) { WhereExpressionList.Add(predicate); return(this); }
public IQuerySet <T> Where <TWhere1, TWhere2>(Expression <Func <TWhere1, TWhere2, bool> > exp) { WhereExpressionList.Add(exp); return(this); }
public IQuerySet <T> Where <TWhere>(Expression <Func <TWhere, bool> > predicate) { WhereExpressionList.Add(predicate); return(this); }
/// <summary> /// 动态化查讯(转换成表达式树集合) 注意,int参数不会判断为0的值 /// </summary> /// <typeparam name="T">对应表</typeparam> /// <param name="dynamicTree"></param> /// <returns></returns> public QuerySet <T> Where(Dictionary <string, DynamicTree> dynamicTree) { if (dynamicTree != null) { foreach (var key in dynamicTree.Keys) { DynamicTree tree = dynamicTree[key]; if (tree != null && !string.IsNullOrEmpty(tree.Value)) { Type tableType = typeof(T); if (!string.IsNullOrEmpty(tree.Table)) { tableType = EntityCache.QueryEntity(tree.Table).Type; } //如果不存在对应表就使用默认表 ParameterExpression param = Expression.Parameter(tableType, "param"); object value = tree.Value; if (value == null) { continue; } else if (tree.ValueType == DbType.DateTime) { value = Convert.ToDateTime(value); } else if (tree.ValueType == DbType.String) { value = Convert.ToString(value); if ("" == value.ToString()) { continue; } } else if (tree.ValueType == DbType.Int32) { int number = Convert.ToInt32(value); value = number; if (0 == number) { continue; } } Expression whereExpress = null; switch (tree.Operators) { case ExpressionType.Equal: //等于 whereExpress = Expression.Equal(Expression.Property(param, tree.Field), Expression.Constant(value)); break; case ExpressionType.GreaterThanOrEqual: //大于等于 whereExpress = Expression.GreaterThanOrEqual(Expression.Property(param, tree.Field), Expression.Constant(value)); break; case ExpressionType.LessThanOrEqual: //小于等于 whereExpress = Expression.LessThanOrEqual(Expression.Property(param, tree.Field), Expression.Constant(value)); break; case ExpressionType.Call: //模糊查询 var method = typeof(string).GetMethodss().FirstOrDefault(x => x.Name.Equals("Contains")); whereExpress = Expression.Call(Expression.Property(param, tree.Field), method, new Expression[] { Expression.Constant(value) }); break; default: whereExpress = Expression.Equal(Expression.Property(param, tree.Field), Expression.Constant(value)); break; } WhereExpressionList.Add(Expression.Lambda(TrimExpression.Trim(whereExpress), param)); } } } return(this); }
public ICommand <T> Where(Expression <Func <T, bool> > predicate) { //WhereExpression = WhereExpression == null ? predicate : ((Expression<Func<T, bool>>)WhereExpression).And(predicate); WhereExpressionList.Add(predicate); return(this); }