public List <QueryPropertyDescription> Provide(QueryBaseModel model)
 {
     return(model.GetType().GetProperties()
            .Where(s => s.GetCustomAttribute <QueryPropertyAttribute>() != null)
            .Select(s => new QueryPropertyDescription(s))
            .ToList());
 }
Ejemplo n.º 2
0
        public Expression <Func <TEntity, bool> > Create(QueryBaseModel model)
        {
            var        param      = this.CreateParameter();
            Expression body       = model.CreateExpressionBody(param);
            var        properties = _queryPropertyProvider.Provide(model);

            foreach (var property in properties)
            {
                var value = property.Property.GetValue(model);

                if (!property.Query.AllowDefault)
                {
                    if (property.Property.PropertyType.IsValueType)
                    {
                        var defaultValue = Activator.CreateInstance(property.Property.PropertyType);
                        if (value.Equals(defaultValue))//务必用equals比较,==是不对的
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (value == null)
                        {
                            continue;
                        }
                    }
                }

                var expression = property.Query.CreateExpression(param, value);
                if (body == null)
                {
                    body = expression;
                }
                else
                {
                    body = Expression.AndAlso(body, expression);
                }
            }
            ;
            Expression <Func <TEntity, bool> > filter = body == null ? s => true : Expression.Lambda <Func <TEntity, bool> >(body, param);

            return(filter);
        }